# JavaScript cheatsheet

Source: https://codewiki.com/cheatsheets/javascript/

## Values

- `const value = source` — bind a block-scoped name that cannot be reassigned
- `let value = source` — bind a block-scoped name that can be reassigned
- `value ?? fallback` — use the fallback only for `null` or `undefined`
- `items.at(-1)` — read the final item without changing the array
- `Object.hasOwn(data, key)` — test an own property explicitly

## Functions

- `const f = (x) => x + 1` — return one expression from an arrow function
- `function f(x) { return x + 1; }` — declare a named function
- `f(...values)` — spread an iterable into arguments
- `f.call(owner, value)` — invoke a function with an explicit receiver
- `f.bind(owner)` — create a function with a bound receiver

## Collections

- `items.map(project)` — build an array by transforming each item
- `items.filter(keep)` — build an array from matching items
- `items.find(match)` — return the first match or `undefined`
- `items.some(match)` — test whether any item matches
- `items.every(match)` — test whether all items match

## Objects

- `{ ...base, key: value }` — make a shallow copy and override one property
- `Object.entries(data)` — read enumerable own string-keyed entries
- `Object.fromEntries(rows)` — build an object from key-value pairs
- `delete data.key` — remove a configurable own property
- `structuredClone(data)` — deep-clone supported structured data

## Errors

- `throw new Error(message)` — raise an explicit failure
- `try { work(); } catch (error) { handle(error); }` — handle a synchronous exception
- `promise.catch(handle)` — handle a rejected promise
- `finally { release(); }` — release resources after either outcome
- `error instanceof TypeError` — test against a known error type

## Async

- `await task` — wait for one promise to settle
- `await Promise.all(tasks)` — fulfill with every result or reject on the first failure
- `await Promise.allSettled(tasks)` — collect every fulfillment and rejection
- `AbortSignal.timeout(ms)` — create a signal that aborts after a timeout
- `for await (const item of source)` — consume an async iterable in sequence

## Modules

- `export { value }` — export a named binding
- `import { value } from './mod.js'` — import a named binding
- `export default value` — export one default value
- `import('./mod.js')` — load a module dynamically and return a promise
- `import.meta.url` — read the current module URL

## Strings

- ```id: ${id}``` — interpolate a value into a template literal
- `text.includes(part)` — test for a substring
- `text.replaceAll(a, b)` — replace every exact string match
- `text.split(separator)` — split text into an array
- `parts.join(separator)` — join values into text

## Boundaries

- `Number.parseInt(text, 10)` — parse a leading base-ten integer
- `Number.isFinite(value)` — test for a finite number without coercion
- `encodeURIComponent(value)` — encode one URL component
- `JSON.parse(text)` — parse JSON text
- `JSON.stringify(value)` — serialize a JSON-compatible value
