Here I will provide all the advanced concepts introuduced from ES6. You can use
let & const
let and const are keywords introduced from ES6.
let – use for variables
const – constants that cannot change once defined
Arrow Functions
Arrow Functions are very convenient way of writing functions in Next Gen JS ES6
******************************** const print = (name) => { console.log(name); }; print('Animesh'); ******************************** const printProfile = (name, age) => { console.log(name+age); }; printProfile('Animesh',35); ******************************** const printName = () => { console.log('Animesh'); }; printName(); ******************************** const multiply = (num1, num2) => num1*num2; console.log(multiply(2,3)); const multiplyConst = (num1) => num1*5; ******************************** console.log(multiplyConst(2)); const multiplyConst2 = num1 => num1*5; console.log(multiplyConst2(2)); ******************************** No return statement is required if you write the function as below: const add = (num1, num2) => (num1+num2) //This function basically returns the sum of 2 numbers when executed as add()
Exports & Imports
Classes
Examples given below:
Class Human {
constructor() {
this.gender = “male”
}
printGender() {
return this.gender
}
}
class Employee extends Human {
constructor() {
super();
//This is the constructor which will be called during instantiation
this.id = “2343242”
}
name = “Animesh”;
const getSalary = () => {
//Logic to get the Employee Salary
}
}
As part of next gen Javascript ES6, it is not required to write the constructor() for a class. Just write as below. It will automatically generate the constructor function and assign the name during instantiation.
class Person {
name = “Animesh”;
}
Spread & Rest Operators
This operator is three dots … It can be used to spread an array or as rest.
Examples:
Spread Operator
const myArr = [1, 2, 3]
const newArr = […myArr, 4]
const newArr2 = [myArr, 4]
console.log(newArr) // Output > [1, 2, 3, 4]
console.log(newArr2) // Output > [[1, 2, 3], 4]
It can be used to spread dictionary objects as well:
const employee = {
name = ‘Animesh’;
}
const newEmployee = {
…employee, age: 37
}
console.log(newEmployee) // It will print both the dictionaries combined
Rest Operator
function printNums(…args) {
return args
}
sortNums(1, 334, 334,3434) // Prints all the arguments passed
=== Symbol
This checks for 2 variables for type and value.
element = 5
while (element === 5) { // This becomes true
}
element = “5”
while (element === 5) { // This becomes false
}
Array and Object Destructuring
Array Destructuring:
[a, b] = [1, 2]
console.log(a) // prints 1
console.log(b) // prints 2
Object Destructuring
{name, age} = {name: ‘Animesh’, age: 37}
console.log(name) // prints Animesh
console.log(age) //prints 37
Use Spread Operator to copy objects
class person = {
name: ‘Animesh’,
age: 37
};
const person2 = person; // This will just create another reference to the same object.
const person3 = {
…person
} // This will copy the object
Array Functions
const names = [‘animesh’, ‘moumita’, ‘aishani’, ‘sudipto’]
const fullNames = names.map((firstName) => {
return firstName + ‘ ‘ + ‘banerjee’
}); // This will return a complete new array with the surnames attached to each element.