Everything here runs in current browsers and Node.js 22.
Variables
_10const total = 10; // can't be reassigned_10let count = 0; // can be reassigned_10count += 1;_10const { name, age = 30 } = user; // destructure_10const [first, ...rest] = list; // array rest
Functions
_10function add(a, b = 1) { return a + b; }_10const double = (n) => n * 2;_10const sum = (...nums) => nums.reduce((a, b) => a + b, 0);_10const greet = ({ name }) => `Hi ${name}`;
Arrays
_10list.map((x) => x * 2); // new array_10list.filter((x) => x > 1); // keep matches_10list.find((x) => x.id === 3); // first match_10list.some((x) => x.done); // any?_10list.every((x) => x.done); // all?_10list.includes(4); // has value?_10list.at(-1); // last item_10list.toSorted((a, b) => a - b); // sorted copy
Objects
_10const copy = { ...user, age: 31 }; // shallow copy_10Object.keys(user); // ['name', 'age']_10Object.entries(user); // [['name', 'Ann'], ...]_10Object.fromEntries(pairs);_10structuredClone(user); // deep copy_10user?.address?.city; // undefined, not error_10user.nickname ?? 'none'; // default if null/undefined
Strings
_10`Total: ${total}`; // template literal_10str.trim();_10str.split(',');_10str.replaceAll('a', 'b');_10str.padStart(3, '0'); // '7' -> '007'_10str.startsWith('http');
Async
_10const res = await fetch('/api/items');_10if (!res.ok) throw new Error(res.statusText);_10const data = await res.json();_10_10await Promise.all([a(), b()]); // all or fail_10await Promise.allSettled([a(), b()]); // every result_10await Promise.race([a(), timeout()]); // first to finish
Errors
_10try {_10 risky();_10} catch (err) {_10 console.error(err.message);_10} finally {_10 cleanUp();_10}_10throw new Error('Bad input', { cause: err });
Classes
_10class Counter {_10 #count = 0; // private field_10 static zero = new Counter();_10 increment() { return ++this.#count; }_10 get value() { return this.#count; }_10}
Modules
_10export const pi = 3.14;_10export default function main() {}_10import main, { pi } from './maths.js';_10const mod = await import('./heavy.js'); // lazy