JavaScript, by example
Modern JavaScript through runnable, annotated examples - from first principles to production-grade patterns.
32 lessons · Updated 2026-04-27
- 01Hello, WorldThe simplest JavaScript program - print a message to the console.
- 02ValuesJavaScript has seven primitive types plus the object type - and one infamous typeof quirk.
- 03VariablesThree ways to declare variables in JavaScript - var, let, and const - each with different scoping and hoisting rules.
- 04Constantsconst locks the binding, not the value - objects and arrays declared with const are still mutable.
- 05NumbersEvery non-BigInt number in JavaScript is a 64-bit float - understanding the precision limits prevents hard-to-debug arithmetic bugs.
- 06StringsJavaScript strings are sequences of UTF-16 code units - template literals, surrogate pairs, and grapheme clusters each have important implications.
- 07Conditionalsif/else, ternary, logical operators as control flow, and switch's non-obvious fallthrough.
- 08Loopsfor, for-of, for-in, forEach - which to use and why forEach can't be awaited.
- 09FunctionsDeclaration vs expression, hoisting, default params, rest params, and dynamic `this`.
- 10Arrow FunctionsShorter syntax and lexical `this` - plus when arrow functions are the wrong choice.
- 11ClosuresFunctions that remember the variables from their defining scope - the backbone of private state, memoization, and the module pattern.
- 12Arraysmap, filter, reduce, and the rest of the essential array API - plus the sort default that bites everyone once.
- 13ObjectsLiterals, shorthand, computed keys, spread, destructuring, and JSON serialization quirks.
- 14Promises and async/awaitAsync control flow with Promise combinators and async/await - plus the concurrency trap that knocks over databases.
- 15ModulesES module import/export, default vs named exports, circular imports, and CJS interop.
- 16BooleansThe true/false type - including the eight falsy values, truthy coercion, and why == is a trap you should never step in.
- 17Higher-Order Functions and CallbacksFunctions that accept or return other functions - the pattern behind map, filter, and every event listener you've ever written.
- 18RecursionFunctions that call themselves - the natural fit for tree traversal, with a hard stack limit that matters on user-supplied data.
- 19Maps and SetsKeyed collections that accept any value as a key (Map) and deduplicated collections (Set) - when to reach for them instead of plain objects and arrays.
- 20DatesCreating, comparing, and formatting dates with the built-in Date object, and why you should store epoch milliseconds or ISO strings and format at the edge.
- 21Classes and `this`The class syntax, prototype-based inheritance under the hood, and the this-rebinding bug that breaks methods the moment they are passed as callbacks.
- 22DOM ManipulationHow JavaScript reads and mutates the live HTML tree - selecting nodes, changing content and classes, and creating elements safely without opening an XSS hole.
- 23EventsAttaching listeners, reading the event object, and cleaning up with AbortController to avoid the memory leaks that forgotten removeEventListener leaves behind.
- 24Form ValidationChecking user input before submission - HTML-native constraints, JavaScript custom messages, and the server-must-re-validate rule that client validation never replaces.
- 25Accessibility BasicsMaking pages work for keyboard users and screen readers - semantic HTML first, focus management for dynamic changes, and ARIA as the patch layer when native elements fall short.
- 26localStorage and sessionStorageBrowser key-value storage that survives page reloads - what it stores, what it doesn't, and why auth tokens and PII must never go near it.
- 27CRUD with localStorageCRUD on a JSON-encoded list stored in a single localStorage key - including try/catch on parse and versioned keys to survive schema changes between app deploys.
- 28Regular ExpressionsRegular expressions match and transform text using patterns built into JS - write them as /foo/ literals or new RegExp("foo"), with flags like g (global) and i (case-insensitive).
- 29Stacks and QueuesStack (LIFO: push/pop) and queue (FIFO: push/shift). Arrays are great stacks but poor queues because shift is O(n); use a linked-list queue when throughput matters.
- 30Linked ListsLinked list: nodes chained via pointers. O(1) insert/remove at known positions, but no random access - walking n nodes to reach index i means arrays win for most application code.
- 31TreesTree: a hierarchy with one root where each node has a value and children. Recursive traversal is elegant but unsafe on deep user data; use iterative DFS with an explicit stack.
- 32GraphsGraph: nodes connected by edges, with cycles and multiple paths. Always carry a visited Set or a cycle will infinite-loop your traversal. Adjacency lists are the right default.
Enjoyed this? Get more essays on software craft delivered to your inbox.
Subscribe free