JS

JavaScript Cheatsheet

Modern JavaScript (ES6+) syntax - array/object methods, destructuring, and async patterns used every day.

Array methods

arr.map(fn)

Transform each element into a new array

arr.filter(fn)

Keep elements matching a condition

arr.reduce(fn, init)

Reduce array to a single value

arr.find(fn)

First element matching a condition

arr.some(fn)

True if any element matches

arr.includes(x)

Check if array contains a value

Destructuring & spread

const { a, b } = obj

Object destructuring

const [a, b] = arr

Array destructuring

{ ...obj, key: value }

Spread into a new object

[...arr1, ...arr2]

Combine arrays with spread

Async patterns

async function fn() {}

Declare an async function

await promise

Wait for a promise to resolve

Promise.all([p1, p2])

Wait for all promises to resolve

try { } catch (e) { }

Handle errors from await calls

Object methods

Object.keys(obj)

Array of an object's keys

Object.values(obj)

Array of an object's values

Object.entries(obj)

Array of [key, value] pairs

Object.assign({}, obj)

Shallow-copy an object