ES2015+ cheatsheet
ES2015+ cheatsheet
io/es6
DEVHINTS.IO Edit
ES2015+ cheatsheet
Design and Development tips in
your inbox. Every weekday.
Block scoping
Let
function fn () {
let x = 0
if (true) {
let x = 1 // only inside this `if`
}
}
Const
const a = 1
let is the new var. Constants work just like let, but can’t be reassigned. See: Let and const
1 af 15 25/04/2025, 00.15
ES2015+ cheatsheet https://devhints.io/es6
Backtick strings
Interpolation
Multiline strings
const str = `
hello
world
`
New methods
"hello".repeat(3)
"hello".includes("ll")
"hello".startsWith("he")
"hello".padStart(8) // " hello"
"hello".padEnd(8) // "hello "
"hello".padEnd(8, '!') // hello!!!
"\u1E9B\u0323".normalize("NFC")
2 af 15 25/04/2025, 00.15
ES2015+ cheatsheet https://devhints.io/es6
Classes
Constructor
constructor (radius) {
this.radius = radius
}
Methods
getArea () {
return Math.PI * 2 * this.radius
}
expand (n) {
return super.expand(n) * Math.PI
}
Static methods
static createFromDiameter(diameter) {
return new Circle(diameter / 2)
}
}
Exponent operator
const byte = 2 ** 8
// Same as: Math.pow(2, 8)
Promises
3 af 15 25/04/2025, 00.15
ES2015+ cheatsheet https://devhints.io/es6
Making promises
Using promises
promise
.then((result) => { ··· })
.catch((error) => { ··· })
promise
.then((result) => { ··· })
.catch((error) => { ··· })
.finally(() => { // logic independent of success/error })
Promise functions
Promise.all(···)
Promise.race(···)
Promise.reject(···)
Promise.resolve(···)
4 af 15 25/04/2025, 00.15
ES2015+ cheatsheet https://devhints.io/es6
Async-await
Destructuring
Destructuring assignment
Arrays
Objects
5 af 15 25/04/2025, 00.15
ES2015+ cheatsheet https://devhints.io/es6
Default values
// Result:
// math === 22, sci === 33, arts === 50
Function arguments
Default values
greet() // Hi Rauno!
greet({ name: 'Larry' }) // Hi Larry!
6 af 15 25/04/2025, 00.15
ES2015+ cheatsheet https://devhints.io/es6
Reassigning keys
Loops
Object destructuring
Extract some keys individually and remaining keys in the object using rest (…) operator
Spread
7 af 15 25/04/2025, 00.15
ES2015+ cheatsheet https://devhints.io/es6
Object spread
const options = {
...defaults,
visible: true
}
The Object spread operator lets you build new objects from other objects.
Array spread
const users = [
...admins,
...editors,
'rstacruz'
]
The spread operator lets you build new arrays in the same way.
Functions
8 af 15 25/04/2025, 00.15
ES2015+ cheatsheet https://devhints.io/es6
Function arguments
Default arguments
Rest arguments
Spread
fn(...[1, 2, 3])
// same as fn(1, 2, 3)
9 af 15 25/04/2025, 00.15
ES2015+ cheatsheet https://devhints.io/es6
Fat arrows
Fat arrows
setTimeout(() => {
···
})
With arguments
Implicit return
numbers.map(n => n * 2)
// No curly braces = implicit return
// Same as: numbers.map(function (n) { return n * 2 })
numbers.map(n => ({
result: n * 2
}))
// Implicitly returning objects requires parentheses around the object
Objects
Shorthand syntax
10 af 15 25/04/2025, 00.15
ES2015+ cheatsheet https://devhints.io/es6
Methods
const App = {
start () {
console.log('running')
}
}
// Same as: App = { start: function () {···} }
const App = {
get closed () {
return this.status === 'closed'
},
set closed (value) {
this.status = value ? 'closed' : 'open'
}
}
11 af 15 25/04/2025, 00.15
ES2015+ cheatsheet https://devhints.io/es6
Extract values
Object.values(fatherJS)
// [57, "Brendan Eich"]
Object.entries(fatherJS)
// [["age", 57], ["name", "Brendan Eich"]]
Modules
Imports
import 'helpers'
// aka: require('···')
12 af 15 25/04/2025, 00.15
ES2015+ cheatsheet https://devhints.io/es6
Exports
Generators
Generators
function* idMaker () {
let id = 0
while (true) { yield id++ }
}
13 af 15 25/04/2025, 00.15
ES2015+ cheatsheet https://devhints.io/es6
For..of iteration
Devhints home
14 af 15 25/04/2025, 00.15
ES2015+ cheatsheet https://devhints.io/es6
15 af 15 25/04/2025, 00.15