JavaScript cheat sheet
JavaScript cheat sheet and syntax reference
A practical JavaScript cheat sheet for syntax, variables, data types, operators, loops, functions, arrays, objects, classes, modules, promises, async/await, DOM, fetch, and regex.
JavaScript quick reference by topic
Variables and constants
Use let for values that change and const for bindings that should not be reassigned.
const name = "Ada"; let count = 0; count += 1;- JavaScript variables
- let const
- declare variable
Data types
JavaScript values include strings, numbers, booleans, null, undefined, bigint, symbols, and objects.
typeof "hello"; typeof 42; value === null;- JavaScript data types
- typeof
- primitive values
Operators and comparisons
Combine arithmetic, assignment, logical operators, and strict equality to write predictable expressions.
total += price; value === expected; isReady && run();- JavaScript operators
- strict equality
- logical operators
Conditions and loops
Control program flow with if, switch, while, for, break, and continue.
if (score >= 90) { grade = "A"; } for (const item of items) { render(item); }- JavaScript if else
- JavaScript loops
- switch statement
Functions
Package reusable behavior with declarations, expressions, parameters, return values, and arrow functions.
function add(a, b) { return a + b; } const double = (n) => n * 2;- JavaScript functions
- arrow function
- return value
Arrays
Store ordered values and transform lists with map, filter, reduce, find, some, and every.
items.map((item) => item.name); items.filter(Boolean); items.reduce((sum, n) => sum + n, 0);- JavaScript arrays
- array methods
- map filter reduce
Objects
Group related data with object literals, properties, methods, destructuring, spread, and optional chaining.
const user = { name: "Ada" }; const { name } = user; user.profile?.email;- JavaScript objects
- object properties
- destructuring
Classes
Create reusable object models with class syntax, constructors, methods, inheritance, static fields, and private fields.
class User { constructor(name) { this.name = name; } } class Admin extends User {}- JavaScript classes
- class syntax
- inheritance
Modules
Split JavaScript into files with named exports, default exports, and imports.
export function format(value) { return String(value); } import { format } from "./format.js";- JavaScript modules
- import export
- ES modules
Promises and async/await
Represent asynchronous results with promises and write promise flow clearly with async functions and await.
const result = await fetchData(); promise.then(handleResult).catch(handleError);- JavaScript promises
- async await
- asynchronous JavaScript
DOM and events
Select elements, update the page, and respond to user actions with browser event listeners.
const button = document.querySelector("button"); button.addEventListener("click", onClick);- JavaScript DOM
- querySelector
- event listener
Fetch and JSON
Call APIs with fetch, parse JSON responses, stringify data, and handle network errors.
const url = "/api/items"; const data = await response.json();- JavaScript fetch
- JSON parse
- API request
Regular expressions
Search, validate, extract, and replace text with RegExp patterns and string methods.
/\d+/.test(value); text.replace(/\s+/g, " ");- JavaScript regex
- regular expression
- RegExp