ES6+ Cheatsheet

Variables

const x = 1Block-scoped constant
let x = 1Block-scoped variable
var x = 1Function-scoped (avoid)

Arrow Functions

const add = (a, b) => a + bConcise arrow function
const fn = x => x * 2Single param, implicit return
const fn = () => ({ key: val })Return object literal

Destructuring

const { a, b } = objObject destructuring
const { a: alias } = objRename while destructuring
const { a = 10 } = objDefault value
const [x, y] = arrArray destructuring
const [first, ...rest] = arrRest in array

Spread & Rest

const merged = { ...a, ...b }Merge objects
const arr2 = [...arr1, 4, 5]Spread array
function f(a, ...rest) {}Rest parameters

Template Literals

`Hello ${name}!`String interpolation
`multi line string`Multi-line string

Classes

class Animal { #name; // private constructor(n) { this.#name = n; } speak() {} }Class with private field
class Dog extends Animal { }Inheritance
static create() {}Static method

Modules

export const x = 1Named export
export default class {}Default export
import { x } from './mod.js'Named import
import MyClass from './mod.js'Default import
import * as mod from './mod.js'Namespace import