DEV Community

Cover image for Some ES-6 Features
Muhammad    Uzair
Muhammad Uzair

Posted on

Some ES-6 Features

Rest Operator

The rest parameter syntax allows us to represent an indefinite number of arguments as an array

 function add(num1,...args){ in args it will return array containing numbers
    return args[0]+args[1]

 }
  console.log(add(1,3,5));   in args there is 3,5 so the sum will be 8

Spread Operator

It will unpack the values of a array,this method will not affect a original element but creates copy

 let arr = ["a","b","c","d"]
 let arr2 = ["e","f","g"]
 let newarr = [...arr , ...arr2];    combine both array into new one
 console.log(newarr); result===>  ["a","b","c","d","e","f","g"]
 newarr.push("h");   it will just push into the new array           
 console.log(newarr);   result===>  ["a","b","c","d","e","f","g","h"]

Template Literals

It is replacement to single/double quotes,it also gives facility to addd variables or do operations,start with backticks instead of quotes

 var para = ` My Name is         
 Uzair `
 console.log(para); It will include line break in result

Interpolate with backticks
Adding variables in between or performing operations

  var  myVariable = `test1`

${} is used to combine variable in it

  var variable2 = `Something ${ myVariable }`  

you can also perform operation in it

 var variable3 = `sum is ${1+2+3} ` you can also perform operation in it
 console.log(variable2 + variable3)   

for of loop
prints all the values of list
iterates all the elements

 let list = [1,2,7,4,5,6] 
  for(let value of list){
    console.log(value); 
  } result ====> 123456

for in loop
it will iterate the length / index not elements

let list = [1,2,7,4,5,6]
       for(i in list){
           console.log(i);
            } 0,1,2,3,4,5   all indexes of array 

Arrow Functions

An arrow function expression is a syntactically compact alternative to a regular function expression,The fat arrows are amazing because they would make your this behave properly, i.e., this will have the same value as in the context of the function

 hello = () => {
     return "Hello World!";
  }

Sets

A Set is a collection of unique elements

const arr = [5, 1, 5, 7, 7, 5];
const unique = [...new Set(arr)]; // [ 5, 1, 7 ]

Maps

The map() method in JavaScript creates an array by calling a specific function on each element present in the parent array.

     var el = document.getElementById('root'); 
     var arr = [2, 5, 6, 3, 8, 9]; 

    var newArr = arr.map(function(val, index){ 
        return {key:index, value:val*val}; 
    })     
    console.log(newArr) 

Hope you find this article useful. Please share your thoughts in the comment section.

Top comments (0)