Menu

JavaScript Cheat Sheet

Last updated

Variables (let, const, var)

Declare values; prefer const, then let, and avoid var.

OperationSyntax
Block-scoped, can reassignlet count = 0;
Block-scoped, cannot reassignconst name = "Ada";
Function-scoped (legacy)var x = 1;
Declare without valuelet result;
Multiple declarationslet 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.

TypeExample
Numberlet n = 42;
Stringlet s = "hi";
Booleanlet ok = true;
Arraylet arr = [1, 2, 3];
Objectlet obj = { id: 1 };
Null / undefinedlet a = null;, let b;
Check a typetypeof n
ConvertNumber("7"), String(42), Boolean(0)

Strings & template literals

Build and manipulate text.

OperationSyntax
Template literalHi ${name}, age ${age} (wrap in backticks)
Lengthtext.length
Upper / lower casetext.toUpperCase(), text.toLowerCase()
Trim whitespacetext.trim()
Replace alltext.replaceAll("a", "b")
Split into an arraytext.split(",")
Includestext.includes("js")
Slicetext.slice(0, 3)

Arrays & array methods

Ordered collections and the methods that iterate them.

OperationSyntax
Add / remove at the endarr.push(4), arr.pop()
Add / remove at the startarr.unshift(0), arr.shift()
Transform each itemarr.map(x => x * 2)
Keep matching itemsarr.filter(x => x > 0)
Reduce to a single valuearr.reduce((sum, x) => sum + x, 0)
Find an itemarr.find(x => x.id === 1)
Test some / everyarr.some(f), arr.every(f)
Index of a valuearr.indexOf(3), arr.includes(3)
Join into a stringarr.join(", ")

Objects

Key-value collections.

OperationSyntax
Createconst user = { id: 1, name: "Ada" };
Access a propertyuser.name, user["name"]
Add / update a propertyuser.age = 25;
Delete a propertydelete user.age;
Get all keys / valuesObject.keys(user), Object.values(user)
Get entriesObject.entries(user)
Merge objectsObject.assign({}, a, b)
Shallow copyconst copy = { ...user };

Control flow

Conditionals and loops.

OperationSyntax
If / else if / elseif (x > 0) { … } else { … }
Ternaryconst y = x > 0 ? 1 : 0;
Switchswitch (x) { case 1: … break; }
For loopfor (let i = 0; i < 5; i++) { … }
For-of (values)for (const item of arr) { … }
For-in (keys)for (const key in obj) { … }
While loopwhile (x < 10) { … }

Functions & arrow functions

Define reusable, callable blocks.

OperationSyntax
Function declarationfunction add(a, b) { return a + b; }
Arrow functionconst add = (a, b) => a + b;
Single argument arrowconst sq = x => x * x;
No arguments arrowconst now = () => Date.now();
Default parameterfunction f(x = 0) { … }
Rest parametersfunction f(...args) { … }
Immediately invoked (IIFE)(() => { … })();

Destructuring & spread

Unpack values and copy/merge structures.

OperationSyntax
Array destructuringconst [a, b] = arr;
Object destructuringconst { id, name } = user;
Rename while unpackingconst { id: userId } = user;
Default valueconst { age = 0 } = user;
Spread an arrayconst all = [...a, ...b];
Spread an objectconst next = { ...user, age: 25 };
Rest in destructuringconst [first, ...rest] = arr;

Promises & async/await

Handle asynchronous work like network requests.

OperationSyntax
Async functionasync function load() { … }
Await a promiseconst data = await fetch(url);
Then / catchpromise.then(res => …).catch(err => …)
Try / catch with awaittry { await f(); } catch (e) { … }
Resolve / rejectPromise.resolve(1), Promise.reject(err)
Wait for allawait Promise.all([a, b])
First to settleawait 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?
Yes. This JavaScript cheat sheet is completely free, with no sign-up required. Bookmark it and come back whenever you need to look up syntax, an array method, or an async pattern.
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.
Can I practice JavaScript online?
Yes. Open the JS playground to run any snippet from this cheat sheet in your browser - nothing to install. When you want structure, Coddy's free interactive JavaScript course takes you from variables and loops to array methods and async/await step by step.
Is this cheat sheet good for beginners?
Yes. It is organized from the basics (variables, data types, strings) down to destructuring and async/await, so you can use the top sections on day one and grow into modern ES6+ features.
Coddy programming languages illustration

Learn JavaScript with Coddy

GET STARTED