JavaScript (ES2020+) Cheatsheet

Modern JS features most interview answers still don't cover.

1 credit

Destructuring & spread

5 items
Object
const { name, email: mail = "" } = user
Array
const [first, ...rest] = items
Rename + default
const { x: posX = 0 } = point
Spread into array
[...a, ...b]
Spread into object
{...base, override: true}

Nullish & optional

3 items
Optional chain
user?.address?.street
Nullish coalesce
value ?? "default" (only null/undefined)
Logical assign
x ??= 0 / obj.a ||= []

Async

javascript
// Parallel fetches
const [users, posts] = await Promise.all([
  fetch("/users").then(r => r.json()),
  fetch("/posts").then(r => r.json()),
]);

// Timeout w/ AbortController
const ctrl = new AbortController();
setTimeout(() => ctrl.abort(), 5_000);
await fetch(url, { signal: ctrl.signal });

Useful array methods

7 items
Map / filter / reduce
[1,2,3].map(x => x*2).filter(x => x > 2).reduce((a,b) => a+b, 0)
Find / findIndex / some / every
arr.find(u => u.id === 1)
flat / flatMap
[[1,2],[3]].flat() → [1,2,3]
Object.entries → for...of
for (const [k, v] of Object.entries(obj)) { ... }
Array.from w/ mapper
Array.from({length: 5}, (_, i) => i*2)
Unique
[...new Set(arr)]
Group (new)
Object.groupBy(users, u => u.role)

Common gotchas

  • `==` coerces — always `===` unless you know why you want coercion.
  • `typeof null === "object"` (historical bug; use `=== null`).
  • `Array(3).fill(0)` is fine; `Array(3)` is sparse (forEach skips).
  • Dates are mutable reference types — clone: `new Date(old)`.

Further reading