Using let
Defines new variables scoped to the nearest block in which it has been declared, without affecting like-named variables outside of the block scope.
Variables using let
are not hoisted to the top
They cannot be redeclared
//before:
var foo = 'bar';
//ES2015
let foo = 'bar';
Using const
Clears up confusion with magic numbers, const
creates a read-only named constants.
Cannot be redefined
Must have an initial value
Scoped to the nearest block and shares similar behaviors as let
///Before:
var luckyNumber = 7;
//ES2015:
const LUCKY_NUMBER = 7;
Variatic Functions Using rest parameters Using spread operator **Arrow Functions or lambdas **:
// Before:
let printName = function(value){
console.log( value );
}
// ES2016:
let printName = (value) => {
console.log(value);
}
Object Instantiation Syntax Object Initializer Shorthand Object Destructuring Shorthand The new object initializer shorthand and object destructuring look very similar, but they are used in different scenarios.* Method Initializer Shorthand String Template & Interpolation
Destructuring & Assigning for...in
let tags = new Set();
tags.add("JavaScript");
tags.add("Programming");
tags.add("Web");
for( let tag of tags){
console.log(`Tag: ${tag}`); //logs "Tag: JavaScript" "Tag: Programming" "Tag: Web"
}
Using Array desctructuring to extract the first element in tags
and assign to the variable first
:
let tags = new Set();
tags.add("JavaScript");
tags.add("Programming");
tags.add("Web");
let [first] = tags;
console.log( `First tag: ${first}` );// logs: "First tag: JavaScript"
The WeakSet
is a more memory efficient type of Set
where only objects are allowed to be stored.