ECMAScript 6 is also known as ES6 which was standardized by the ECMA International.
In this blog, we will discuss the below concepts
- Arrow Function
- Exponentiation Operator
- Spread Operator
Let’s start with one by one
- Arrow Function
An arrow function is the shortest syntax to define a function
In Normal Javascript, we define the function like this:-
function testAddition(x,y) {
return x + y;
}
But in ES6 we can define the function like below:-
var x = (x,y) => x+y;
2. Exponentiation Operator
An Exponentiation Operator(**) returns the power of the first value according to the second value.
For example:-
alert(2**2);
The above code will output 4.
3. Spread Operator
Spread operator allows the developer to iterate the javascript array and perform some action like:- copy one array to another array, add some new index value in the array, etc
For example:-
In normal javascript, we iterate the array like this:-
//define the result variable here
var result = 1;
//define the array here
var test_arr = [1,2,3,4,5];
//initialize the varaible here
var j;
//for loop starts
for(j = 0; j < test_arr.length;j++) {
result *= test_arr[j];
}
alert(result);
In ES6:-
var spread_arr = {1,2,3,4,5};
var test = (a,b,c,d,e) => a*b*c*d*e;
alert(test(...spread_arr));
There are many other features which I explained in future.
Be the first to comment.