ES2015+ cheatsheet
ES2015+ cheatsheet
ES2015+ cheatsheet
ads via Carbon
A quick overview of new JavaScript features in ES2015, ES2016, ES2017, ES2018 and beyond.
function fn () { "hello".repeat(3)
let x = 0 "hello".includes("ll")
if (true) { "hello".startsWith("he")
let x = 1 // only inside this `if` "hello".padStart(8) // " hello"
} "hello".padEnd(8) // "hello "
} "hello".padEnd(8, '!') // hello!!!
"\u1E9B\u0323".normalize("NFC")
Const
let is the new var. Constants work just like let, but can’t be reassigned. See: Let and
const
Classes
Interpolation
constructor (radius) {
const message = `Hello ${name}` this.radius = radius
}
Multiline strings
Methods
const str = `
hello getArea () {
world return Math.PI * 2 * this.radius
` }
static createFromDiameter(diameter) {
let bin = 0b1010010 return new Circle(diameter / 2)
let oct = 0o755 }
}
https://devhints.io/es6 1/5
05/12/2024, 11:57 ES2015+ cheatsheet
Exponent operator
const byte = 2 ** 8
// Same as: Math.pow(2, 8)
Promises
Using promises
Promise functions
promise
.then((result) => { ··· })
.catch((error) => { ··· }) Promise.all(···)
Promise.race(···)
Promise.reject(···)
Promise.resolve(···)
Destructuring
https://devhints.io/es6 2/5
05/12/2024, 11:57 ES2015+ cheatsheet
function greet({ name = 'Rauno' } = {}) { function printCoordinates({ left: x, top: y }) { for (let {title, artist} of songs) {
console.log(`Hi ${name}!`); console.log(`x: ${x}, y: ${y}`) ···
} } }
greet() // Hi Rauno! printCoordinates({ left: 25, top: 90 }) The assignment expressions work in loops, too.
greet({ name: 'Larry' }) // Hi Larry!
Object destructuring
Spread
The Object spread operator lets you build new objects from other objects.
The spread operator lets you build new arrays in the same way.
See: Object spread
See: Spread operator
Functions
https://devhints.io/es6 3/5
05/12/2024, 11:57 ES2015+ cheatsheet
Objects
Methods
See: Object literal enhancements
const App = {
start () {
console.log('running')
}
Computed property names
}
// Same as: App = { start: function () {···} }
let event = 'click'
let handlers = {
See: Object literal enhancements [`on${event}`]: true
}
// Same as: handlers = { 'onclick': true }
https://devhints.io/es6 4/5
05/12/2024, 11:57 ES2015+ cheatsheet
Extract values
Object.values(fatherJS)
// [57, "Brendan Eich"]
Object.entries(fatherJS)
// [["age", 57], ["name", "Brendan Eich"]]
Modules
Imports Exports
import * as Helpers from 'helpers' export is the new module.exports. See: Module exports
// aka: const Helpers = require('···')
Generators
https://devhints.io/es6 5/5