ES6 brings new features to JavaScript

Posted on 08-11-2018 by Nadir Latif


Source: http://qnimate.com/post-series/ecmascript-6-complete-tutorial/

The ES6 standard has introduced several changes to JavaScript. These changes include Classes, Proxies, Promises, Modules, Iterators and Generators and more

The for of syntax allows iterating over an array. The following example shows how it works:


var array = [4, 5, 3, 7, 9];

//'i' refers to the values of the array indexes
for(var i of array)
{
    console.log(i); //4, 5, 3, 7, 9
}

The spread operator which is "..." allows expanding an array. It can be used to allow functions to take indefinite number of arguments. For example:


//args variable is an array holding the passed function arguments
function function_one(...args)
{   
    console.log(args);
    console.log(args.length);
}

function_one(1, 4);
function_one(1, 4, 7);
function_one(1, 4, 7, 0);

ES6 also allows functions to return multiple values. It allows a function to return a numerically indexed array. Its values can be assigned to multiple variables. For example:


function function_name()
{
    return [6, 2, 3, 4, 5, 3]; //here we are storing variables in an array and returning the array
}

var a, b, c, d, e, f;

[a, b, c, d, e, f] = function_name();

The const keyword is used to declare constant variables. A variable prefixed with const, cannot be updated. Also we cannot declare more than one variable with same name and scope as constant