Updated April 3, 2023
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.
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:
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:
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 –