ES6 - Cheat Sheet
ES6 - Cheat Sheet
Arrow Function
console.log(sum(2,6)) // prints 8
Default Parameters
function print(a = 5) {
console.log(a)
print() // prints 5
print(22) // prints 22
www.gomycode.co 01
Let Scope
let a = 3
if (true) {
let a = 5
console.log(a) // prints 5
}
console.log(a) // prints 3
Const
// can be assigned only once
const a = 55
a = 44 // throws an error
www.gomycode.co 02
Multi-line String
console.log(
`This is a
multi-line string`
Template String
console.log(message)
www.gomycode.co 03
Exponent Operator
const byte = 2 ** 8
Spread Operator
const a = [ 1, 2 ]
const b = [ 3, 4 ]
console.log(c)
// [1, 2, 3, 4]
www.gomycode.co 04
String Includes()
console.log('scripts'.includes('scripts'))
// prints true
console.log('scripts'.includes('prints'))
// prints false
console.log('scripts'.includes('Scripts'))
// prints false
www.gomycode.co 05
String startsWith()
console.log('scripts'.includes('sc'))
// prints true
console.log('scripts'.includes('rt'))
// prints false
String repeat()
console.log('st'.repeat(3))
// prints "ststst"
Destructuring array
let [a,b] = [3,7];
console.log(a); // 3
console.log(b); // 7
www.gomycode.co 06
Destructuring object
let obj = {
a: 77,
b: 66
};
console.log(a); // 77
console.log(b); // 66
const obj = { a, b }
// Before es6:
// obj = { a: a, b:b }
console.log(obj)
// prints { a:2, b:5 }
www.gomycode.co 07
Object.assign()
const obj1 = { a: 1 }
const obj2 = { b: 2 }
obj1, obj2)
console.log(obj3)
// { a: 1, b: 2 }
.finally(() => {
/* logic independent of success/error */
})
/* The handler is called when the promise is fulfilled or rejected. */
www.gomycode.co 08
Spread Operator
const a = {
firstName: "FirstName",
lastName: "LastName1",
}
const b = {
...a,
lastName: "LastName2",
canSing: true,
}
console.log(a)
//{firstName: "FirstName", lastName: "LastName1"}
console.log(b)
/* {firstName: "FirstName", lastName: "LastName2",
canSign: true} */
/* great for modifying objects without side effects/affecting the original */
www.gomycode.co 09
Destructuring Nested objects
const Person = {
age: 23,
sex: "male",
maritalstatus: "single",
address: {
country: "BD",
state: "Dhaka",
city: "N.Ganj",
pincode: "123456",
},
};
www.gomycode.co
.com
console.log(name, state, pincode)
// Rezaul Karim Dhaka 123456
console.log(city) // ReferenceError
a: 5,
b() {
console.log('b')
www.gomycode.co
.com
Object.entries()
const obj = {
firstName: "FirstName",
lastName: "LastName1",
age: 23,
country: "Bangladesh",
};
console.log(entries)
/* prints [
['firstName', 'FirstName'],
['lastName', 'LastName'],
['age', 23],
['country', 'Bangladesh']
]; */
www.gomycode.co
.com