Skip to content

JavaScript Cheat Sheet

Last verified September 2026 — runs in your browser

JavaScript Cheatsheet

Search code, tasks or categories in English or Spanish. Each entry has a link to save or share it.

Showing 79 of 79 snippets

Modern JavaScript. Patterns need your application variables; DOM needs a browser and import/export a module. Complete examples include their own data.

Sort numerically ascending

Arrays · Complete example
const numbers = [10, 2, 1];
const sorted = [...numbers].sort((a, b) => a - b);
console.log(sorted); // [1, 2, 10]
console.log(numbers); // [10, 2, 1]

sort() mutates its receiver. Copy first to preserve the input; without a comparator, sorting uses strings.

Documentation

Deep clone an object

Objects · Pattern / syntax
structuredClone(obj)

Supports structured-cloneable values, not functions or DOM nodes; unsupported values throw DataCloneError.

Documentation

Async/await function

Promises · Pattern / syntax
async function fetchJson(url) {
  const res = await fetch(url);
  if (!res.ok) throw new Error(`HTTP ${res.status}`);
  return res.json();
}

Pass a JSON endpoint and handle rejection at the call site. Browser requests must satisfy CORS.

Documentation

Fetch with .then() chain

Promises · Pattern / syntax
fetch(url)
  .then(r => {
    if (!r.ok) throw new Error(`HTTP ${r.status}`);
    return r.json();
  })
  .then(console.log)
  .catch(console.error);

Create a new promise

Promises · Complete example
const promise = new Promise(resolve => {
  setTimeout(() => resolve(42), 100);
});
promise.then(console.log); // 42

Check start/end

Strings · Pattern / syntax
s.startsWith("pre");
s.endsWith("suf");

Each expression returns its own boolean.

for...of loop

ES6+ · Pattern / syntax
for (const item of iterable) {}

Create a Proxy

ES6+ · Pattern / syntax
const proxy = new Proxy(target, handler)

JavaScript Cheat Sheet — JS Reference & Syntax Guide

Find JavaScript syntax for arrays, objects, functions, promises, strings and the DOM. Search categories in English or Spanish, copy code or link directly to an entry. Complete examples include data; patterns need variables from your application.

Common pitfalls in JavaScript

Numeric sorting needs a comparator. sort() changes the array it is called on; the complete example copies the input first. fetch() can fulfill on an HTTP error, so check response.ok before reading JSON. DOM snippets run in a browser, while import and export require a module. Spread and Object.assign make shallow copies.

  • 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

Free. No signup. Your inputs stay in your browser. Ads via Google AdSense (consent required).

By ·