ES6 Samples

  • How to add key, value pair in the existing array.
var employeeList = [{id:'1', name:'A'},{id:'2', name:'B'}];
var isPermanent = {flag:true};
var newEmployeeList = ids.map((eachObj) => ({...eachObj, ...isPermanent}));
console.log(newEmployeeList);

image

From var to const / let

  • const - You can use it for all variables whose values never change.
  • let – for variables whose values do change.
  • Avoid var

template literals

  • String interpolation
const first = "SURESH";
const last = "A";
console.log(`(${first} ${last})`);
  • Multi-line strings
const HTML5_SKELETON = `
    <!doctype html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
    </body>
    </html>`;

From for to forEach() to for-of

Prior to ES5, you iterated over Arrays as follows:

var arr = ['a', 'b', 'c'];
for (var i=0; i<arr.length; i++) {
    var elem = arr[i];
    console.log(elem);
}

In ES5, you have the option of using the Array method forEach()

arr.forEach(function (elem) {
    console.log(elem);
});

In ES6, the for-of loop combines both advantages:

const arr = ['a', 'b', 'c'];
for (const elem of arr) {
    console.log(elem);
}

If you want both index and value of each array element,

var employee =[{id:1, name:'A'},{id:2, name:'B'}];
for (const [index, elem] of employee.entries()) {
    console.log(index+'. '+elem.name);
}