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(); });
}