0% found this document useful (0 votes)
409 views

JavaScript ES6 - Cheat Sheet

Uploaded by

Diego Do Santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
409 views

JavaScript ES6 - Cheat Sheet

Uploaded by

Diego Do Santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ES6 cheat sheet

Arrow function Default parameters


const sum = (a, b) => a + b function print(a = 5) {
console.log(a)
console.log(sum(2, 6)) // prints 8 }

print() // prints 5
print(22) // prints 22

let scope const


let a = 3 // can be assigned only once:
const a = 55
if (true) {
let a = 5 a = 44 // throws an error
console.log(a) // prints 5
}

console.log(a) // prints 3

Multiline string Template strings


console.log(` const name = 'Leon'
This is a const message = `Hello ${name}`
multiline string
`) console.log(message) // prints "Hello Leon"

String includes() String startsWith()


console.log('apple'.includes('pl')) // prints true console.log('apple'.startsWith('ap')) // prints true
console.log('apple'.includes('tt')) // prints false console.log('apple'.startsWith('bb')) // prints
false

String repeat() Destructuring array


console.log('ab'.repeat(3)) // prints "ababab" let [a, b] = [3, 7];

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 }

let { a, b } = obj; // Before es6:


// obj = { a: a, b: b }
console.log(a); // 55
console.log(b); // 44 console.log(obj) // prints { a: 2, b: 5 }

object function assignement spread operator


const obj = { const a = [ 1, 2 ]
a: 5, const b = [ 3, 4 ]
b() {
console.log('b') const c = [ ...a, ...b ]
}
} console.log(c) // [1, 2, 3, 4]

obj.b() // prints "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 } };

const entries = Object.entries(obj);


/* returns an array of [key, value]
pairs of the object passed
*/

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

Exponent operator Promises with finally


const byte = 2 ** 8 promise
.then((result) => { ··· })
// Same as: Math.pow(2, 8) .catch((error) => { ··· })
.finally(() => { // logic independent of
success/error })

// The handler is called when the promise is


fulfilled or rejected.

Copyright © 2018 - 2020


www.developer-cheatsheets.com

You might also like