JavaScript Cheatsheet

JavaScript Cheatsheet
const x = 10

Block-scoped constant

Variables
let y = 20

Block-scoped variable

Variables
typeof x

Get type as string

Variables
const { a, b } = obj

Object destructuring

Variables
const [a, b] = arr

Array destructuring

Variables
const merged = { ...obj1, ...obj2 }

Spread/merge objects

Variables
const copy = [...arr]

Spread/copy array

Variables
x ?? fallback

Nullish coalescing (null/undefined only)

Variables
obj?.prop?.nested

Optional chaining

Variables
[1, 2, 3]

Create an array

Arrays
arr.push(item)

Add item to end

Arrays
arr.pop()

Remove and return last item

Arrays
arr.shift() / arr.unshift(item)

Remove first / add to start

Arrays
arr.map(x => x * 2)

Transform each element

Arrays
arr.filter(x => x > 0)

Filter elements by condition

Arrays
arr.reduce((acc, x) => acc + x, 0)

Reduce to single value

Arrays
arr.find(x => x.id === 1)

Find first matching element

Arrays
arr.findIndex(x => x > 5)

Find index of first match

Arrays
arr.some(x => x > 0)

Check if any element matches

Arrays
arr.every(x => x > 0)

Check if all elements match

Arrays
arr.includes(item)

Check if item exists

Arrays
arr.flat(Infinity)

Flatten nested arrays

Arrays
arr.sort((a, b) => a - b)

Sort numerically ascending

Arrays
arr.slice(1, 3)

Get subarray (non-mutating)

Arrays
arr.splice(1, 2, ...items)

Remove/insert at index (mutating)

Arrays
[...new Set(arr)]

Remove duplicates

Arrays
Array.from({ length: 5 }, (_, i) => i)

Create array from range

Arrays
const obj = { key: "value" }

Create an object

Objects
Object.keys(obj)

Get array of keys

Objects
Object.values(obj)

Get array of values

Objects
Object.entries(obj)

Get array of [key, value] pairs

Objects
Object.assign({}, obj1, obj2)

Merge objects (shallow)

Objects
structuredClone(obj)

Deep clone an object

Objects
Object.freeze(obj)

Make object immutable (shallow)

Objects
"key" in obj

Check if key exists

Objects
delete obj.key

Delete a property

Objects
const { unwanted, ...rest } = obj

Omit a key via destructuring

Objects
function fn(x) { return x }

Function declaration

Functions
const fn = (x) => x * 2

Arrow function

Functions
const fn = (x = 10) => x

Default parameter

Functions
const fn = (...args) => args

Rest parameters

Functions
fn(1, ...arr)

Spread arguments

Functions
setTimeout(fn, 1000)

Delay execution by ms

Functions
setInterval(fn, 1000)

Repeat execution every ms

Functions
function* gen() { yield 1 }

Generator function

Functions
async function fn() { const res = await fetch(url); return res.json(); }

Async/await function

Promises
fetch(url).then(r => r.json())

Fetch with .then() chain

Promises
Promise.all([p1, p2])

Wait for all promises

Promises
Promise.race([p1, p2])

First promise to resolve/reject

Promises
Promise.allSettled([p1, p2])

Wait for all (resolved or rejected)

Promises
new Promise((resolve, reject) => { ... })

Create a new promise

Promises
try { await fn(); } catch (e) { console.error(e); }

Async error handling

Promises
`Hello ${name}`

Template literal

Strings
s.includes("sub")

Check if substring exists

Strings
s.startsWith("pre") / s.endsWith("suf")

Check start/end

Strings
s.split(",")

Split into array

Strings
s.trim()

Remove whitespace from both ends

Strings
s.replace(/regex/g, "new")

Replace with regex (global)

Strings
s.padStart(5, "0")

Pad start to length

Strings
s.repeat(3)

Repeat string N times

Strings
s.match(/pattern/g)

Find all regex matches

Strings
s.at(-1)

Get last character

Strings
document.querySelector(".class")

Select first matching element

DOM
document.querySelectorAll("div")

Select all matching elements

DOM
el.addEventListener("click", fn)

Add event listener

DOM
el.classList.add("active")

Add CSS class

DOM
el.classList.toggle("hidden")

Toggle CSS class

DOM
el.textContent = "text"

Set text content (safe)

DOM
el.setAttribute("data-id", "5")

Set HTML attribute

DOM
el.style.color = "red"

Set inline style

DOM
el.remove()

Remove element from DOM

DOM
const m = new Map()

Create a Map (any key type)

ES6+
const s = new Set([1, 2, 3])

Create a Set (unique values)

ES6+
for (const [k, v] of map) {}

Iterate over Map entries

ES6+
for (const item of iterable) {}

for...of loop

ES6+
Symbol("desc")

Create unique symbol

ES6+
import { fn } from "./module.js"

ES module import

ES6+
export default fn

Default export

ES6+
const proxy = new Proxy(target, handler)

Create a Proxy

ES6+
Showing 79 of 79 snippets

Free Online JavaScript Cheatsheet

A searchable, filterable JavaScript quick reference. Covers variables, arrays, objects, functions, promises, DOM manipulation, strings, and modern ES6+ features. Copy any snippet instantly.

About this cheatsheet

A comprehensive JavaScript cheatsheet covering modern JS patterns and syntax.

  • 75+ practical JavaScript snippets
  • 9 categories including DOM and ES6+
  • Search across code and descriptions
  • Filter by category
  • One-click copy to clipboard
  • Covers modern ES2023+ features

100% free. No signup required. No data collected or sent anywhere.

Explore All Tools