home
es6 cheat sheet
Ecmascript 6
print

Date : March 2017

Let

Let (in contrast with var) is block scoped.

let snack = 'Meow Mix';

function getFood(food) {
    if (food) {
        let snack = 'Friskies';
    }
    return snack;
}

getFood(true); // 'Meow Mix'

Const

Immutable value.

const MY_CONST = 23;
MY_CONST = 12; // Error
const MY_CONST = 2; // Error
var MY_CONST = 4; // Error
let MY_CONST = 12; // Error

Template string

var a = 26;
var b = 25;
console.log("The best drink ever is " + ( a + b ) + " and not " + ((a * b * 3) - 286) + ".");
// VS
console.log(`The best drink ever is ${ a + b } and not ${(a * b * 3) - 286}.`);

Tagged function

function modifier(strings,...values) {
    // strings = ['The best drink ever is', 'and not', '.'];
    // values = 51, 1664
    return "Modified words";
}
console.log(modifier`The best drink ever is ${ a + b } and not ${(a * b * 3) - 286}.`);

Arrow function

((variable) => {
    console.log(variable);
})('test');
((doubleMe) => doubleMe * 2)(2) // auto return 4;
((doubleMe) => {return doubleMe * 2})(2) // return 4; 

Arrow functions don't have its own this.

For of

let arr = [1, 2, 3];
for (const i of arr) {
    console.log(i);
}
// "1", "2", and "3"

For in

let arr = [3, 5, 7];
for (let i in arr) {
  console.log(i);     // show indexes : "0", "1", "2"
}
arr.toto = "coucou";
Object.prototype.objCustom = function () {};
for (let i in arr) {
  console.log(i);     // show : "0", "1", "2", "toto", "objCustom"
}
var obj = {a:1, b:2, c:3};
 
for (var prop in obj) {
  console.log("obj." + prop + " = " + obj[prop]);
}
// "obj.a = 1"
// "obj.b = 2"
// "obj.c = 3"

Promise

function makeIt() {
    return new Promise((resolve, reject) => { makeAsyncTask(); resolve(); });
}

Destructuring

[a, b] = [1, 2];
a === 1;
b === 2;
 
[a, b, ...c] = [1, 2, 3, 4, 5];
a === 1;
b === 2;
c === [3, 4, 5];
 
{a, b} = {a:1, b:2};
a === 1;
b === 2;

Default values :

var a, b;
[a = 7, b = 1] = [5];
 
a === 5;
b === 1;

Destructuring on the fly

for (let {name: n, family: { mother: f } } of persons) {
    console.log("Name : " + n + ", Mother : " + f);
}
 
function showPersonne({name: n, family: { mother: f } }) {
    console.log("Name : " + n + ", Mother : " + f);
}

Rest & Spread operator

var arr = [1, 2, 3];
var arr1 = [4, 5];
var arr2 = [...arr, 4];
arr.push(...arr1);

function f(a, b, ...args) {}

function f(...[a, b, c]) {}

Default parameters

function multiplier(a, b = a + 1) {
    return a*b;
}

Class

class Bridge {
    constructor(width, height) {
        this.width = width;
        this.height = height;
    }
    get width() { return this.width; }
    set width(value) { 
        this.width = value; 
        console.log('width changed'); 
    }
}

class Viaduc extends Bridge {
    constructor(width, height, weight) {
        super(width, height);
        this.weight = weight;
    }
    
    static staticCalcArea (width, height) { 
        return width * height 
    }
    calcArea () { return this.width * this.height }
}

var monViaduc = new Viaduc(1, 2, 3);
monViaduc.width = 2; // width changed

Set

var mySet = new Set();
mySet.add(1);
mySet.add(1); //ignored => duplicate value isn't allowed 
mySet.add("du texte");
mySet.has(1); //true
mySet.size; //2
mySet.delete(1);

Map

var myMap = new Map();
myMap.set(1, 1);
myMap.set("du texte", 2);
myMap.set(o, "test");
myMap.has("du texte"); //true
myMap.delete("du texte");