by DannyUpdated 9 sections

Everything here runs in current browsers and Node.js 22.

Download the PDF

Variables


_10
const total = 10; // can't be reassigned
_10
let count = 0; // can be reassigned
_10
count += 1;
_10
const { name, age = 30 } = user; // destructure
_10
const [first, ...rest] = list; // array rest

Functions


_10
function add(a, b = 1) { return a + b; }
_10
const double = (n) => n * 2;
_10
const sum = (...nums) => nums.reduce((a, b) => a + b, 0);
_10
const greet = ({ name }) => `Hi ${name}`;

Arrays


_10
list.map((x) => x * 2); // new array
_10
list.filter((x) => x > 1); // keep matches
_10
list.find((x) => x.id === 3); // first match
_10
list.some((x) => x.done); // any?
_10
list.every((x) => x.done); // all?
_10
list.includes(4); // has value?
_10
list.at(-1); // last item
_10
list.toSorted((a, b) => a - b); // sorted copy

Objects


_10
const copy = { ...user, age: 31 }; // shallow copy
_10
Object.keys(user); // ['name', 'age']
_10
Object.entries(user); // [['name', 'Ann'], ...]
_10
Object.fromEntries(pairs);
_10
structuredClone(user); // deep copy
_10
user?.address?.city; // undefined, not error
_10
user.nickname ?? 'none'; // default if null/undefined

Strings


_10
`Total: ${total}`; // template literal
_10
str.trim();
_10
str.split(',');
_10
str.replaceAll('a', 'b');
_10
str.padStart(3, '0'); // '7' -> '007'
_10
str.startsWith('http');

Async


_10
const res = await fetch('/api/items');
_10
if (!res.ok) throw new Error(res.statusText);
_10
const data = await res.json();
_10
_10
await Promise.all([a(), b()]); // all or fail
_10
await Promise.allSettled([a(), b()]); // every result
_10
await Promise.race([a(), timeout()]); // first to finish

Errors


_10
try {
_10
risky();
_10
} catch (err) {
_10
console.error(err.message);
_10
} finally {
_10
cleanUp();
_10
}
_10
throw new Error('Bad input', { cause: err });

Classes


_10
class Counter {
_10
#count = 0; // private field
_10
static zero = new Counter();
_10
increment() { return ++this.#count; }
_10
get value() { return this.#count; }
_10
}

Modules


_10
export const pi = 3.14;
_10
export default function main() {}
_10
import main, { pi } from './maths.js';
_10
const mod = await import('./heavy.js'); // lazy