Examples
JavaScript examples
Add behavior to the web and build applications.
JavaScript Variables
Use const and let for values.
JavaScript Functions
Write reusable logic.
JavaScript conditions and array loops
Filter values with strict comparisons and accumulate a total.
JavaScript map, filter, and reduce
Build a transformation pipeline without mutating the source.
JavaScript types, equality, and coercion
Know the primitive types, why === is safer than ==, and which values count as false.
JavaScript closures and scope
Use a function that remembers its surrounding variables, and see why let fixes the classic loop bug.
JavaScript destructuring, spread, and rest
Unpack objects and arrays into variables, copy with spread, and collect arguments with rest.
JavaScript string methods and regular expressions
Split, pad, replace, and extract parts of text with strings and regular expressions.
JavaScript promises, async, and await
Write asynchronous code that reads like synchronous code, and handle errors with try and catch.
JavaScript classes and private fields
Protect instance state with a private class field.
JavaScript inheritance and overriding
Reuse initialization and specialize instance behavior.
JavaScript prototypes and class inheritance
See that classes are built on prototype chains, and extend one constructor from another.
JavaScript private fields, getters, and static members
Hide state with #private fields, expose it through a getter, and keep a counter on the class itself.
JavaScript binary search
Search a sorted numeric array with a shrinking interval.
JavaScript Map and Set
Count values and identify distinct entries.
JavaScript linked lists and iterators
Build a singly linked list class that can be looped over with for...of.
JavaScript stacks and queues
Use an array as a stack, and write a queue that does not shift the whole array on every removal.
JavaScript sorting and the default sort pitfall
Learn why [10, 9, 1].sort() is wrong, sort with comparators, and write a merge sort.
JavaScript binary search trees with a generator
Insert values, search, and produce the sorted order lazily with a recursive generator.
JavaScript graphs: BFS, DFS, and path existence
Store a graph as a Map of neighbour lists and search it breadth-first and depth-first.
JavaScript: memoization trades memory for time
Count the calls made by a naive recursive Fibonacci and a memoized one.
JavaScript: counting the steps of binary search
Show that searching a sorted array of 1000 items takes at most about ten comparisons.
JavaScript: an index turns a scan into a lookup
Compare scanning an array for a row with looking it up in a Map keyed by id.
JavaScript: GROUP BY and HAVING with Map
Total a column per group, keep the groups that pass a condition, and sort them.
JavaScript: the event loop as a cooperative scheduler
Predict the order in which synchronous code, nextTick, promises, and timers run in Node.js.
JavaScript: LRU replacement with a Map
Use Map's insertion order to evict the least recently used page and count page faults.
JavaScript: an HTTP server and client on the loopback interface
Start a Node.js HTTP server on a free port and request it with fetch to see status codes.
JavaScript: a TCP echo server with the net module
Send a line through a TCP socket and print the server's reply.
JavaScript: unit tests with node:assert
Write small tests with assertions, including checks that bad input throws the right error.
JavaScript: the observer pattern with EventEmitter
Let several listeners react to the same event without the emitter knowing about them.
JavaScript: SHA-256 hashing and HMAC with node:crypto
Hash a message, authenticate it with a secret key, and compare tags in constant time.
JavaScript: storing passwords with scrypt and a salt
Derive a memory-hard hash from a password and a random salt, then verify a login attempt.
JavaScript: preventing XSS by escaping output
Turn special HTML characters into entities so user text is shown as text, never run as markup.
JavaScript: concurrency without parallelism
See how async functions interleave on a single thread and finish together under Promise.all.
JavaScript: real parallelism with worker threads
Split an array between two worker threads and add their partial results.
JavaScript: breadth-first search for the fewest steps
Find a shortest path in an unweighted graph by exploring it layer by layer.
JavaScript: constraint satisfaction by backtracking
Color a map so that neighbouring regions differ, counting every valid coloring.
JavaScript: querying facts stored as triples
Represent knowledge as subject–predicate–object triples and answer queries with variables.
JavaScript: proof by resolution refutation
Show that a knowledge base entails a query by deriving a contradiction from the knowledge base plus the negated query.
JavaScript: fitting a line with least squares
Compute the slope, intercept, and R² of the best-fit line in closed form.
JavaScript: a naive Bayes text classifier
Classify short messages as spam or ham from word counts with Laplace smoothing.
JavaScript: a forward pass through two dense layers
Multiply by weight matrices, add biases, apply ReLU, and finish with softmax probabilities.
JavaScript: checking a gradient numerically
Compare a hand-derived gradient with a finite-difference estimate, the standard test for backpropagation code.
JavaScript: RGBA pixel arrays and Uint8ClampedArray
Convert pixels stored the way a canvas stores them to grayscale and see how clamping works.
JavaScript: a sharpen filter and why clamping matters
Apply a sharpening kernel to a step edge and see the overshoot clamped to the valid range.