env.dev

JavaScript Cheat Sheet — ES6+ Quick Reference

JavaScript ES6+ quick reference: destructuring, spread operator, arrow functions, array methods, promises, async/await, and modules.

By env.dev Updated

A quick reference for modern JavaScript (ES6+). Covers destructuring, spread/rest, arrow functions, array methods, promises, async/await, modules, and template literals — current through ES2025 (approved June 2025), which added Iterator helpers, Set methods, and Promise.try.

Destructuring

SyntaxDescription
const { a, b } = objObject destructuring
const { a: x } = objDestructure with rename
const { a = 10 } = objDestructure with default value
const { a, ...rest } = objDestructure with rest
const [a, b] = arrArray destructuring
const [a, , c] = arrSkip elements in array
const [a, ...rest] = arrArray destructure with rest
function f({ name, age }) {}Destructure in function params

Spread & Rest

SyntaxDescription
[...arr1, ...arr2]Merge arrays
{ ...obj1, ...obj2 }Merge objects (shallow)
[...arr]Shallow copy array
{ ...obj }Shallow copy object
function f(...args) {}Rest parameters (variadic)
Math.max(...nums)Spread array as function arguments
[...new Set(arr)]Remove duplicates from array

Arrow Functions

SyntaxDescription
const f = (a, b) => a + bImplicit return (expression body)
const f = (a) => { return a * 2 }Block body with explicit return
const f = a => a + 1Single param (parens optional)
const f = () => 42No params
const f = () => ({ key: val })Return object literal (wrap in parens)
arr.map(x => x * 2)Inline callback
arr.filter((_, i) => i % 2 === 0)Use index parameter

Array Methods

MethodDescription
arr.map(fn)Transform each element, return new array
arr.filter(fn)Keep elements where fn returns true
arr.reduce(fn, init)Reduce to single value with accumulator
arr.find(fn)Return first element matching predicate
arr.findIndex(fn)Return index of first match or -1
arr.some(fn)True if any element passes test
arr.every(fn)True if all elements pass test
arr.flat(depth)Flatten nested arrays to depth
arr.flatMap(fn)Map then flatten one level

Promises

PatternDescription
new Promise((resolve, reject) => {})Create a promise
promise.then(val => {})Handle fulfilled value
promise.catch(err => {})Handle rejection
promise.finally(() => {})Run after settled (fulfilled or rejected)
Promise.all([p1, p2])Wait for all, fail on first rejection — use when results depend on each other
Promise.allSettled([p1, p2])Wait for all, never short-circuits — use for independent tasks
Promise.race([p1, p2])Resolve/reject with first settled — common for timeouts
Promise.any([p1, p2])Resolve with first fulfilled, ignore rejections until all fail
Promise.try(fn)Run fn, wrap sync throws as rejections (ES2025)

Async / Await

PatternDescription
async function f() {}Declare async function
const result = await promiseWait for promise to resolve
try { await p } catch (err) {}Error handling with async/await
const results = await Promise.all(ps)Await multiple promises in parallel
for await (const x of stream) {}Async iteration
await Promise.allSettled(ps)Await all regardless of outcome

Modules (ESM)

SyntaxDescription
export const x = 1Named export
export function f() {}Named function export
export default function() {}Default export
import { x } from './mod.js'Named import
import f from './mod.js'Default import
import { x as y } from './mod.js'Import with rename
import * as mod from './mod.js'Namespace import
const mod = await import('./mod.js')Dynamic import — lazy-load for code splitting

Template Literals

SyntaxDescription
`Hello ${name}`String interpolation
`${a + b}`Expression evaluation
`line1\nline2`Multiline strings
tag`hello ${x}`Tagged template literal
`${cond ? "a" : "b"}`Ternary inside template
String.raw`\n`Raw string (no escape processing)

Common gotchas

  • arr.sort() compares as strings by default — [10, 9, 1].sort() gives [1, 10, 9]. Pass (a, b) => a - b, and prefer toSorted() (ES2023) which returns a copy instead of mutating in place.
  • == coerces types: 0 == "" and null == undefined are both true. Always use ===. And NaN === NaN is false — use Number.isNaN().
  • typeof null returns "object" — a bug standardized since 1995. Check value === null explicitly.
  • Spread and Object.assign copy one level deep — nested objects are shared references. Use structuredClone() (all modern runtimes) for a real deep copy.
  • Date parsing of non-ISO strings is implementation-defined, and months are zero-indexed. Temporal reached TC39 Stage 4 in March 2026 (ES2026) as the replacement — until it lands everywhere, parse timestamps explicitly (see Unix timestamps explained).

Related: constants in JavaScript, the TypeScript cheat sheet and the JSON cheat sheet.

Was this helpful?

Frequently Asked Questions

What is the difference between let, const, and var?

const declares block-scoped constants that cannot be reassigned. let declares block-scoped variables. var is function-scoped and hoisted — avoid it in modern code.

What are arrow functions?

Arrow functions (=>) provide a shorter syntax for functions and do not have their own this binding. Example: const add = (a, b) => a + b.

What is destructuring?

Destructuring extracts values from arrays or properties from objects into variables. Example: const {name, age} = person; or const [first, ...rest] = array.