Kayavak / The Web Frontend / FE·15 JavaScript / Deeper
Deeper · The Web Frontend
JavaScript
The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.
Why does this exist?
In May 1995 Netscape asked Brendan Eich for a scripting language for the browser, simple enough for designers, that
"looked like Java" for marketing reasons. He wrote the first version in about ten days. It borrowed syntax from Java,
first-class functions from Scheme and prototypes from Self, and its rushed edges (type coercion, ==, typeof null)
have been with us ever since, because the web can't break old pages.
Netscape submitted it to Ecma for standardisation (ECMAScript, 1997). After a long stall, ES5 (2009) tidied the
language, and ES2015 transformed it: let and const, classes, modules, promises, arrow functions. Since then a
smaller edition ships every year. Two other events made JavaScript the world's most widely deployed language:
Gmail and Google Maps (2004–05) showed what in-page requests could do ("Ajax", named in 2005), and Node.js (Ryan Dahl,
2009) took V8 out of the browser and onto servers.
The mental model
Try it yourself first: write your own model in the Mental Model Explorer, then compare.
JavaScript runs on one thread per page, taking one task at a time from a queue. Nothing runs "in the background" in your code: asynchronous work is the browser doing things for you and queueing a callback when it's done.
| Question | Answer |
|---|---|
| What is it? | A dynamically typed language with first-class functions, prototypes and an event-loop runtime |
| Problem it solves | Making pages respond to people without blocking them, in a language anyone could start writing |
| Parts | Values and types, scopes and closures, objects and prototypes, promises and async functions, the event loop |
| How it works | Run a task to completion, then all microtasks (promise reactions), then maybe render, then the next task |
| Assumptions | Each task is short; nothing else touches your variables while your code runs |
| When they change | A long task blocks input and rendering: the page freezes, which is why INP measures it (ch27) |
| Where it fails | CPU-heavy work on the main thread (move it to a worker, ch29); coercion surprises; this binding |
| Related ideas | Closures are lexical scope (Foundations, ch02); the event loop is non-blocking I/O (Foundations, ch08) |
What if?
What if JavaScript were multi-threaded with shared memory?
Reason it through, then open
Every DOM operation would need locks, every library would need to be thread-safe, and every page would be open to
data races, the hardest bugs in programming (Foundations, ch08). The single-threaded, run-to-completion model means
your code can't be interrupted halfway through updating something. Workers add parallelism without that cost by
sharing nothing by default and passing messages; SharedArrayBuffer is the opt-in exception, and it's gated
behind cross-origin isolation.
What if await blocked the thread until the result arrived?
Reason it through, then open
The whole page would freeze for every network request. await doesn't block: it ends the current function's run,
returns control to the event loop, and resumes the function in a later microtask when the promise settles. Other code
can run in between, which is also why state you read before an await may have changed after it.
Where the boundary is
| Aspect | Task (macrotask) | Microtask |
|---|---|---|
| Comes from | Events, timers, network callbacks, messages | Promise reactions, queueMicrotask, await resumptions |
| When it runs | One per turn of the event loop | All of them, right after the current task, before rendering |
| Danger | A long one blocks input | An endless chain of them starves rendering and tasks |
| Confused because | Both are "later"; microtasks are much sooner |
How sure are we?
| Claim | How it's known | Where from |
|---|---|---|
| The first version was written in about ten days in May 1995 | Historical | Eich's own accounts |
| All microtasks run before the next task | Convention | The HTML event loop specification |
typeof null === "object" is a bug kept for compatibility |
Historical | A proposal to fix it was rejected because it broke existing pages |
| JavaScript is single-threaded | Simplification | Each agent (page, worker) is; a page can start many workers |
Still open
Types are the live question: a proposal to let JavaScript engines ignore type annotations ("types as comments") has been discussed at TC39 since 2022, and Node.js can now strip TypeScript types natively. Whether the language itself should gain them is unsettled.
What would change your mind? If an engine could run shared-memory threads on the main thread without data races reaching authors, the single-thread model would no longer be necessary. Nothing suggests that's near.
Recall
When do microtasks run?
After the current task finishes and before the next task or render, all of them.
Does `await` block the thread?
No; it suspends the function and lets the event loop run other work until the promise settles.
Why is JavaScript's main-thread model single-threaded?
To avoid data races in the DOM and in every page's code; workers add parallelism by message passing.
Why can't old JavaScript mistakes like `typeof null` be fixed?
Changing them would break existing pages, and the web doesn't break old content.
Reflect
Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.