JavaScript
The language of the browser and, through Node.js, of servers. Dynamic, function-first, and single-threaded on an event loop.
Level: intermediate
What is it?
JavaScript is a dynamically typed, prototype-based language with first-class functions. It runs in every browser and, through Node.js, Deno, and Bun, on servers. It executes on a single-threaded event loop: long-running work blocks the loop, so I/O is asynchronous through callbacks, promises, and async/await.
Why does it matter?
It is the only language browsers run natively, so it sits at the center of web development. Understanding the event loop, closures, and asynchronous flow is what separates writing JavaScript from fighting it.
Mental model
A single waiter in a restaurant. He takes an order, hands it to the kitchen, and moves on to the next table instead of standing there. When the kitchen rings the bell (a promise resolves), he returns with the dish. He is fast because he never waits — but if he stops to cook one dish himself, every table waits.
How it works
- Values are numbers (IEEE doubles), strings, booleans, null, undefined, symbols, bigints, and objects. There is no separate integer type.
- Functions are values, and closures capture the variables around them.
- The event loop runs one task at a time; promises and async/await schedule continuations without blocking it.
- Use === and !== to avoid surprising coercions from == (0 == '' is true).
- Modules (import/export) organize code; npm is the ecosystem's package manager.
- Modern engines (V8, SpiderMonkey, JavaScriptCore) JIT-compile hot code, so JavaScript can be fast.
Steps
- Call an async function
- Start I/O and return control
- Event loop runs other work
- I/O completes and queues a callback
- Promise resolves and code continues
Code example: async/await over a promise
async function getJson(url) {
const res = await fetch(url);
if (!res.ok) throw new Error("HTTP " + res.status);
return res.json();
}
getJson("https://example.com/api/items")
.then((items) => console.log(items.length))
.catch((err) => console.error(err.message));Real-world usage
- Interactive web pages and single-page apps
- Node.js servers and tooling
- Scripting in editors, games, and automation
Common mistakes
- Blocking the event loop with a long synchronous computation.
- Forgetting to await a promise or to handle its rejection.
- Depending on == coercion, or on 'this' without understanding how it is bound.
Interview questions
How does the JavaScript event loop work?
JavaScript runs one call stack. Async operations hand work to the environment; when they finish, callbacks are queued. Microtasks (promise continuations) run before the next macrotask, and the loop picks the next task only when the stack is empty.
What is a closure?
A function together with the variables from the scope where it was defined. It keeps access to those variables after the outer function returns.
== versus ===?
== converts types before comparing; === requires equal type and value. Prefer ===.