ES6- Most Important New Features
1. Arrow Functions
We can write a function without a function keyword now. In special case, we don’t need to return keyword and curly braces also. The writing function is now so simple.
//ES5
function sum1(num1,num2){
return num1+num2;
}
console.log(sum1(2,3)) //5//ES6
const sum2 = (num1,num2) => num1+num2
console.log(sum2(2,3)) //5
2. Classes:
Finally, we have found the class keyword in ES6. We can create a class blueprint of Object by creating a class. Using the constructor() method we have to initialize property.
class Animal{
constructor(name,leg){
this.name = name;
this.leg = leg;
}
}let animal1 = new Animal(“dog”,4)
let animal2 = new Animal(“Cat”,4)console.log(animal1.name,animal2.name) //’dog’ ‘Cat’
3. Let keyword
We can now declare block-level scope using the let keyword.
let name = “Sakib”
{
let name =”Noman”
console.log(name) //’Noman’
}console.log(name) //’Sakib’
4. Const keyword
We can declare block-level const value using the const keyword. We can’t change the value of a const variable in the same block.
const name = “Sakib”
{
const name =”Noman”
const name =”Abdullah”
console.log(name) /*Identifier ‘name’ has already been declared*/
}console.log(name) //’Sakib’
5. Promises
Sometimes we have to wait for data. When we find data then we can do something with that data. JavaScript handles this situation by promises. A promise is a JavaScript object.
Follow the code: Here myPromise need 3 seconds to produce data, so we have to wait three seconds to find this data.
let myPromise = new Promise((Resolve, Reject)=>{
setTimeout(()=>{
Resolve(“I love You !!”)}, 3000);
});myPromise.then((value)=>{
console.log(value);
});
6. array.findIndex() method
array.findIndex() return index of first element that is conditionally matched. If no element found it return -1
var numbers = [2, 9, 25, 29];var firstIndex = numbers.findIndex((value, index, array)=> value < 10);console.log(firstIndex) //0
7. Default parameters
Sometimes there is a chance to forget to pass an argument. But for this reason, an application may be crashed. To solve this problem ES6 has default parameters. If we don’t pass the argument intentionally or by mistake default value will initialize by default
const myFunction = (value1, value2 = 1) => value1/value2myFunction(2) //2
8. Rest parameter
Sometimes we need to pass an unknown number of argument as an array. We can use the rest parameter to solve this problem.
const sum = (…args) => {
let sum = 0;
for (let arg of args) sum += arg;
return sum;
}let x = sum(4, 9, 1, 2);
let y = sum(1,2,3)
let z = sum(3,4,5,6)console.log(x,y,z) //16 6 18
9. array.find()
We can find an element from an array conditionally by using this method.
var numbers = [4, 9, 16, 25, 29];var first = numbers.find((value,index,array)=>value>18);console.log(first) //25
10. Global Methods
isFinite() and isNaN() are two global method introduced in ES6.
isFinite() return false if number is Infinity or Nan. and isNan() return false if number is not NaN.