ES6
1. ES6 is a new standardized interpretation of JavaScript that was released in 2015.
2. It's also referred to as ECMAScript 2015.
3. ES6 is the base for ultramodern programming languages like Angular and ReactJs. With Object- Acquainted Classes, Arrow Functions, String Literals, and far more.
4. ES6 is a point-rich advancement to ES5, the former interpretation of JavaScript.
The following is a list of top ES6 features that every JavaScript developer should be aware of.
Default parameters
Destructuring assignment
let and const
1. Default parameter
This can be achieved with default function parameters in JavaScript since the default value for function parameters is undefined.
Example:
Before ES6
Function addNumber (num1,num2){
Return num1+num2;
};
addNumber(10,5) //return 15
addNumber(5) //return NaN as num2 is undefined
With ES6
Function addNumber (num1, num2 =10){
Return num1+num2;
};
addNumber(10,5) //return 15
addNumber(5) //return 15 as num2 has default value=10
Default function parameters allow being initialized with default values if no value or undefined is passed.
2. Destructuring assignment
1. The 2 most used data structures in JavaScript are Object and Array.
2. Destructuring assignment is a special syntax that allows us to “unpack values” from arrays/properties from objects into a bunch of variables.
3. Simplification of restructuring is to break down a complex structure into simpler parts.
4. Destructuring Assignment defined on the left-hand side.
Array destructuring
Example: 1.
Let X, Y:
[X, Y] = [10,20]
console.log(x); //10
console.log(y) //20
Object destructuring
Example: 2.
({X, Y} = {X=10, Y=20});
console.log(x); //10
console.log(y) //20
3. Let and const
Let
1. Let and const are block scoped.
2. Variable declared as let are in the block scope
Example:
For(let i=0; i<3; i++){
Console.log(i);
}
Console.log(i)
Output
1
2
3
3. Hosting is not allowed
Example:
X=8
Console.log(x);
Let x;
Output
error
Cannot access 'X before initialization
4. Let is allowing to reassign values.
Example:
Let V1 =1;
V1 = 30;
Console.log(v1);
Output
30
5. Let are not allowed to redeclaration of the variable
Example:
Let V1 =1;
Let V1 = 30;
Console.log(v1);
Output
error
Uncaught SyntaxError: Identifier 'v1' has already been declared
Const
1. Variable declared as const are in the block scope
Example
{
const x = 2;
console.log(x);
} console.log(x);
Output
2
Error
Uncaught ReferenceError: x is not defined
2. Hosting is not allowed
Example:
X=8
Console.log(x);
const x;
Output
error
Cannot access 'X before initialization
3. Const is not allowing to reassign value.
Example:
const v1 = 1;
v1 = 30;
console.log(v1);
Output
error
Uncaught Type Error: Assignment to constant variable.
4. Const is not allowed to redeclaration of the variable
const v1 = 1;
const v1 = 30;
console.log(v1)
Output
error
Uncaught SyntaxError: Identifier 'v1' has already been declared
No comments:
Post a Comment