JavaScript Cheat Sheet
Last updated
Variables (let, const, var)
Declare values; prefer const, then let, and avoid var.
| Operation | Syntax |
|---|---|
| Block-scoped, can reassign | let count = 0; |
| Block-scoped, cannot reassign | const name = "Ada"; |
| Function-scoped (legacy) | var x = 1; |
| Declare without value | let result; |
| Multiple declarations | let a = 1, b = 2; |
| Constant object (mutable contents) | const user = {}; user.id = 1; |
Data types
The primitive and reference types plus how to check them.
| Type | Example |
|---|---|
| Number | let n = 42; |
| String | let s = "hi"; |
| Boolean | let ok = true; |
| Array | let arr = [1, 2, 3]; |
| Object | let obj = { id: 1 }; |
| Null / undefined | let a = null;, let b; |
| Check a type | typeof n |
| Convert | Number("7"), String(42), Boolean(0) |
Strings & template literals
Build and manipulate text.
| Operation | Syntax |
|---|---|
| Template literal | Hi ${name}, age ${age} (wrap in backticks) |
| Length | text.length |
| Upper / lower case | text.toUpperCase(), text.toLowerCase() |
| Trim whitespace | text.trim() |
| Replace all | text.replaceAll("a", "b") |
| Split into an array | text.split(",") |
| Includes | text.includes("js") |
| Slice | text.slice(0, 3) |
Arrays & array methods
Ordered collections and the methods that iterate them.
| Operation | Syntax |
|---|---|
| Add / remove at the end | arr.push(4), arr.pop() |
| Add / remove at the start | arr.unshift(0), arr.shift() |
| Transform each item | arr.map(x => x * 2) |
| Keep matching items | arr.filter(x => x > 0) |
| Reduce to a single value | arr.reduce((sum, x) => sum + x, 0) |
| Find an item | arr.find(x => x.id === 1) |
| Test some / every | arr.some(f), arr.every(f) |
| Index of a value | arr.indexOf(3), arr.includes(3) |
| Join into a string | arr.join(", ") |
Objects
Key-value collections.
| Operation | Syntax |
|---|---|
| Create | const user = { id: 1, name: "Ada" }; |
| Access a property | user.name, user["name"] |
| Add / update a property | user.age = 25; |
| Delete a property | delete user.age; |
| Get all keys / values | Object.keys(user), Object.values(user) |
| Get entries | Object.entries(user) |
| Merge objects | Object.assign({}, a, b) |
| Shallow copy | const copy = { ...user }; |
Control flow
Conditionals and loops.
| Operation | Syntax |
|---|---|
| If / else if / else | if (x > 0) { … } else { … } |
| Ternary | const y = x > 0 ? 1 : 0; |
| Switch | switch (x) { case 1: … break; } |
| For loop | for (let i = 0; i < 5; i++) { … } |
| For-of (values) | for (const item of arr) { … } |
| For-in (keys) | for (const key in obj) { … } |
| While loop | while (x < 10) { … } |
Functions & arrow functions
Define reusable, callable blocks.
| Operation | Syntax |
|---|---|
| Function declaration | function add(a, b) { return a + b; } |
| Arrow function | const add = (a, b) => a + b; |
| Single argument arrow | const sq = x => x * x; |
| No arguments arrow | const now = () => Date.now(); |
| Default parameter | function f(x = 0) { … } |
| Rest parameters | function f(...args) { … } |
| Immediately invoked (IIFE) | (() => { … })(); |
Destructuring & spread
Unpack values and copy/merge structures.
| Operation | Syntax |
|---|---|
| Array destructuring | const [a, b] = arr; |
| Object destructuring | const { id, name } = user; |
| Rename while unpacking | const { id: userId } = user; |
| Default value | const { age = 0 } = user; |
| Spread an array | const all = [...a, ...b]; |
| Spread an object | const next = { ...user, age: 25 }; |
| Rest in destructuring | const [first, ...rest] = arr; |
Promises & async/await
Handle asynchronous work like network requests.
| Operation | Syntax |
|---|---|
| Async function | async function load() { … } |
| Await a promise | const data = await fetch(url); |
| Then / catch | promise.then(res => …).catch(err => …) |
| Try / catch with await | try { await f(); } catch (e) { … } |
| Resolve / reject | Promise.resolve(1), Promise.reject(err) |
| Wait for all | await Promise.all([a, b]) |
| First to settle | await Promise.race([a, b]) |
The modern JavaScript syntax you reach for most, on one page. This JavaScript cheat sheet is a quick reference for everyday ES6+ - declaring variables, string and array methods, objects, arrow functions, destructuring, and async/await.
Everything here is standard JavaScript that runs in modern browsers and Node.js. Copy what you need, or try any snippet live in the JS playground - nothing to install.
JavaScript cheat sheet FAQ
Is this JavaScript cheat sheet free?
What is the difference between == and === in JavaScript?
== is loose equality: it converts the operands to the same type before comparing, so 0 == "0" is true. === is strict equality: it compares both value and type without conversion, so 0 === "0" is false. Prefer === (and !==) in almost all cases - it is predictable and avoids surprising coercions.What is the difference between let, const, and var?
const and let are block-scoped and were added in ES6; var is function-scoped and older. Use const for values you never reassign, let for values you do, and avoid var to prevent hoisting and scope bugs. Note that a const object or array can still have its contents changed - only the binding is constant.