EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials ES6 Tutorial ES6 JavaScript
Secondary Sidebar
ES6 Tutorial
  • ES6 Basic and Advance
    • What is ES6
    • ES6 Features
    • ES6 Enum
    • ES6 Array Methods
    • ES6 Spread Operator
    • ES6 Operator
    • ES6 Proxy
    • ES6 New Features
    • ES6 Set
    • ES6 reduce
    • ES6 Destructuring
    • ES6 filter
    • ES6 Cheat Sheet
    • ES6 Template Strings
    • ES6 Generators
    • ES6 Default Parameters
    • ES6 Object Destructuring
    • ES6 Arrow Function
    • ES6 Array
    • ES6 CLASSES
    • ES6 JavaScript
    • ES6 modules

ES6 JavaScript

ES6 JavaScript

Definition of ES6 JavaScript

ES6 JavaScript is defined as one of the versions of a programming language that is standardized at the levels of ECMA International, an organization that is a non-profit and deals with communications and information systems and is used for general purposes. ECMAScript 2015 or ES2015 was called a significant update in terms of the JavaScript programming language. The last update that happened was in 2009, named ES5 and hence the subsequent version was tagged as ES6. ES6 has transformed the way web development and the components related to that have been made easier to deal with. Be it generators, arrow functions, rest or spread operators, etc.

What is ES6 JavaScript?

By now we know that ES6 is the updated version of its earlier one, with a lot of new, cool and necessary features critical for developers to make their lives simpler. Elaborating on the ECMAScript, it is a scripting language that shapes the platform of JavaScript. The scripting language is standardized by the ECMA International organization in accordance to the specifications of ECMA-262 and ECMA-402. In this article, we will look at the commonly used feature in the space of ES6 JavaScript.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,268 ratings)

How to Use ES6?

To start with understanding the use of ES6, in this tutorial we will assume that we have Node Package Manager and node.js as a part of the development environment. Post that we will follow the corresponding steps:

Step 1: Setting up the project folder

1. A new folder is first created.
2. Open a terminal and then change the directory of execution in the terminal to the root directory.
3. The command npm init -y is executed so that the project is initialized as npm package.

Step 2: Creating the entry point to the App. In this step, in the root directory, we create a file named node.js.

Step 3: Installation of required and optional dependencies and/or devdependencies. In this step, we will assume that the modules required is for the development of the express app.

1. We need to save/install the dependencies viz. express (A minimalistic web framework that is compatible with Node.js) and morgan (A middleware logger that is used for HTTP logging) by executing the command npm install –save morgan express.
2. Some development dependencies are installed in order to use the babel compiler in the project. The required babel dependencies are installed by using the command npm install –save-dev <dependencies’ name>.

Step 4: Creation of babel configuration file in the root directory

1. The command touch .babelrc is run in order to create the configuration file.
2. The .babelrc is opened and the below code is pasted in the file.

{
"presets": ["es2015"]
}

Step 5:

1. In this step the package.json is edited to include a start command and a new build command.

With these, we are now ready to use ES6 for building JavaScript features in the express app.

Arrow functions

Arrow functions is the feature that gets listed in the ES6 version of JavaScript. This process allows developers to create functions in a much cleaner way in comparison to the regular functions. We use the let function for declaring variables in JavaScript. The variables can also be declared by using the var keyword as well except the fact that the let keyword makes the variables block-scoped i.e. the variables are just accessed within a particular block. In the below example we will look at how we can transform a function expression in a regular sense to an arrow function.

Function to return addition of 2 variables:

let a = function(x, y) {
return x + y;
}

Can be transformed to the below arrow function:

let a = (x, y) => x + y;

The general syntax is as follows:

let < Function name > = (< arg 1 > , < arg 2 >, ... < arg N >) => expression
}

Default parameters

Similar to the introduction of the arrow function in ES6, we have the concept of default parameter is also introduced in the ES6 version which allows developers to pass some default parameters to the variable in the function. In case, the variable doesn’t receive a variable as a part of the function call, the default value gets assigned for all the corresponding calls. For example, let us take 2 examples within the same function call.

function sum (x = 2, y = 7) {
return x + y;
}
console.log(sum(19, 91)); // We would get the result as 19 + 91 = 110
console.log(sum(9));// We would get the result as 9 + 7 (default value in function) = 16

es6 JavaScript Import and export

Similar to the above features we discussed, ES6 also provides ways of importing and exporting one or many members in a module. These members can be classes, functions, variables, and/or objects. Let us look at the export and import functionalities for each of the individual members of the module.

Syntax for Export of:

Variables:

export let < name of the variable >;

Function:

export function < name of the function > () {
// Statements that needs to be executed in this function
}

Class:

export class < name of the class > {
constructor() {
// Statements that needs to be executed in this class
}
}

The syntax for import: A variable can be imported using the keyword import by writing:

import < importing member > from “< location of the js file >”;

Examples

Example #1

Sum of numbers using arrow functions.

Syntax:

let sum = (x, y) => console.log(x + y);
console.log("Sum of the 2 numbers is: ");
sum(9,11);

Output:

O

Example #2

Sum of the numbers using arrow functions

Syntax:

let sum = (x=27, y=9) => console.log(x + y);
console.log("Sum of the 2 numbers when both numbers are passed: ");
sum(19,91);
console.log("Sum of the 2 numbers when ONLY first number is passed: ");
sum(1991);

Output:

I

Conclusion

We now come to the end of the article that explains the ES6 in JavaScript along with new features that are added in this block of the release of ES6 version and also have a hands-on understanding of the features in ES6.

Recommended Articles

This is a guide to ES6 JavaScript. Here we discuss the definition, what is ES6, How to Use ES6? examples with code implementation. You may also have a look at the following articles to learn more –

  1. Javascript Object Key
  2. JavaScript max
  3. JavaScript return
  4. JavaScript String to Date
Popular Course in this category
JavaScript Training Program (39 Courses, 24 Projects, 4 Quizzes)
  39 Online Courses |  24 Hands-on Projects |  230+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more