JavaScript ES6 - Tuong-Nguyen/Angular-D3-Cometd GitHub Wiki

New Scope

let - const vs var Block scope {} vs Function scope & hoisting

(function () {
    console.log(block);

    if(true){
        let block = "block";
    }
})();
  • block variable is defined in the if block
  • block variable can be used after its declaration.

Default variable

function logSomething(something = 'default message', somethingElse = something + ' with something') {
    console.log(something, somethingElse);
}

// do not pass argument - default values are used.
logSomething();
logSomething('hello', ' and overriding');

Rest variable

Put arguments into an array

    function join(...items) {
        return items.join(' ');
    }

    console.log(join(1 , 2, 3));

Spread operator ...

Break an array into individual elements

    function sum(a, b, c) {
        return a + b + c;
    }

    console.log(sum(...[1, 2, 3]));

Template string

  • Multiple lines
  • Interpolation ${}
  • Tag string: prefix a template string with a function
// multiple line
let multiple = `this is
multiple line string`;

// Interpolate expression into string
let a = 3;
let s = `a = ${a}`;

// Tag string
// stringArrays: array of string separated by interpolations
// values: interpolations values
function str(stringArrays, ...values){
   return values[0] + stringArrays[0];
}

str`a = ${3 + 5}`;

Practices

http://es6katas.org/