JavaScript Cheat Sheet

A quick reference for modern JavaScript syntax, methods, and patterns.

Data Types & Variables

Primitivesstring, number, boolean, null, undefined, symbol, bigint
typeoftypeof 'hello' → 'string'; typeof 42 → 'number'
letlet x = 10 (block-scoped, can reassign)
constconst x = 10 (block-scoped, cannot reassign)
varvar x = 10 (function-scoped, avoid in modern code)

Array Methods

maparr.map(x => x * 2) — transform each element
filterarr.filter(x => x > 5) — keep matching elements
reducearr.reduce((sum, x) => sum + x, 0) — accumulate values
findarr.find(x => x.id === 3) — first match
some/everyarr.some(x => x > 5) / arr.every(x => x > 0)
includesarr.includes(5) — check existence
flatMaparr.flatMap(x => [x, x * 2]) — map then flatten

Async Patterns

Promiseconst p = new Promise((resolve, reject) => { ... })
async/awaitconst data = await fetch(url)
Promise.allconst [a, b] = await Promise.all([p1, p2])
Promise.allSettledResolves when all settle (resolve or reject)
try/catchtry { await fn() } catch (err) { ... }
Error handlingWrap async calls in try/catch or .catch()