ES6 Cheatsheet PDF
ES6 Cheatsheet PDF
Cheat Sheet
Constants let vs var
> console.log(val)
New Types // -> Throws ReferenceError
> let val = 3
Symbols, Maps, WeakMaps and Sets > console.log(val) // -> 3
> {
… let cue = 'Luke, I am your father'
> setTimeout(() => {
… console.log(cue)
… console.log(‘delayed’)
… }
… }, 1000)
> 'Luke, I am your father'
> (function () {
> setTimeout(function () { … var cue = 'Luke, I am your father'
… console.log(‘delayed’) … console.log(cue) // 'Luke, I am –
… }.bind(this), 1000) … }())
> console.log(cue) // Reference Error
Object Notation Novelties String Interpolation, Thanks to Template Literals
// Computed properties
> let key = new Date().getTime()
> const name = 'Tiger'
> let obj = { [key]: “value” }
> const age = 13
> obj
> console.log(`My cat is named ${name} and is
> { '1459958882881': 'value' }
${age} years old.`)
> My cat is named Tiger and is 13 years old.
> let [a, b, c, d] = [1, 2, 3, 4]; > let luke = { occupation: 'jedi',
> console.log(a); father: 'anakin' }
> 1 > let {occupation, father} = luke
> b > console.log(occupation, father)
> 2 > jedi anakin
Spread Operator
...Go Destructuring Like a Boss
Generators
They return a objects that implement an iteration protocol. i.e. it has a next() method that returns { value: < some
value>, done: <true or false> }.
print() // prints 5
print(22) // prints 22
console.log(a) // prints 3
console.log(a); // 3
console.log(b); // 7
Destructuring object object property assignement
let obj = { const a = 2
a: 55, const b = 5
b: 44
}; const obj = { a, b }
Object.assign() Object.entries()
const obj1 = { a: 1 } const obj = {
const obj2 = { b: 2 } firstName: 'Vipul',
lastName: 'Rawat',
const obj3 = Object.assign({}, obj1, obj2) age: 22,
country: 'India',
console.log(obj3) // { a: 1, b: 2 } };
console.log(entries);
/* prints
[
['firstName', 'Vipul'],
['lastName', 'Rawat'],
['age', 22],
['country', 'India']
];
*/
spread operator Destructuring Nested Objects
const a = { const Person = {
firstName: "Barry", name: "John Snow",
lastName: "Manilow", age: 29,
} sex: "male",
materialStatus: "single",
const b = { address: {
...a, country: "Westeros",
lastName: "White", state: "The Crownlands",
canSing: true, city: "Kings Landing",
} pinCode: "500014",
},
console.log(a) // {firstName: "Barry", lastName: };
"Manilow"}
const { address : { state, pinCode }, name } =
console.log(b) // {firstName: "Barry", lastName: Person;
"White", canSing: true}
console.log(name, state, pinCode) // John Snow
// great for modifying objects without side The Crownlands 500014
effects/affecting the original console.log(city) // ReferenceError
Copyright © 2018
www.developer-cheatsheets.com