JavaScript
JavaScript is a single-threaded, dynamically typed, prototype-based language with first-class
functions and a managed heap. Almost every “JavaScript question” in an interview is really a
question about one of those five properties: what runs when (the event loop), what a function can
still see (closures), what this points at, how objects inherit, and how asynchronous work is
scheduled.
This section covers the parts of the language that separate people who use React from people who understand what React is doing.
The 60-second answer
Section titled “The 60-second answer”“JavaScript runs on one call stack per agent. Slow work is pushed to the host — the browser’s Web
APIs or Node’s libuv thread pool — and comes back as a callback on a task queue. Between tasks the
engine drains a microtask queue, which is where promise callbacks run. Functions are objects and
close over their lexical scope, so state can be captured without classes. Objects inherit through a
prototype chain, which is what class syntax compiles down to. And since there is no shared
memory between threads in normal code, concurrency is cooperative: you yield by awaiting or
returning, never by blocking.”
The language model in five ideas
Section titled “The language model in five ideas”| Idea | What it means | Where it bites |
|---|---|---|
| Single call stack | One function executes at a time | A long loop freezes the tab or the server |
| Event loop | The host schedules callbacks onto queues | Ordering bugs between timers and promises |
| Lexical scope | Scope is decided by where code is written | Closures keep memory alive |
| Prototypes | Property lookup walks a chain of objects | class is sugar; this is dynamic |
| Dynamic types | No compile-time checking | TypeScript exists to fill this gap |
What interviews actually test
Section titled “What interviews actually test”- Ordering. Predict the output of a snippet mixing
setTimeout,Promise.thenand synchronous logs. This is the single most common JavaScript screen question. - Closures. Write a counter, a
oncewrapper, a memoiser, or explain why three handlers log the same index. this. Explain why a method passed as a callback loses its receiver.- Promises. Difference between
all,allSettled,raceandany; sequential versus parallel awaits. - Immutability and reference semantics. Why
[...arr]is not a deep copy, why===on objects compares identity. - The DOM and events. Delegation, bubbling,
preventDefaultversusstopPropagation.
How this section is organised
Section titled “How this section is organised”- The Event Loop — call stack, task and microtask queues, timers, Node versus browser, starvation.
- Closures and Scope — lexical scope, the module pattern, loop pitfalls, memory.
- this, Prototypes and Classes
— binding rules,
call/apply/bind, the prototype chain,classand inheritance. - Async JavaScript — callbacks,
promises,
async/await, combinators, cancellation.
If you are preparing for a TypeScript role, read this section first and then TypeScript: the type system sits on top of these semantics, and most “TypeScript errors” are really JavaScript misunderstandings.
A five-minute self-check
Section titled “A five-minute self-check”Predict the output before reading the answer.
console.log('A');setTimeout(() => console.log('B'), 0);Promise.resolve().then(() => console.log('C'));queueMicrotask(() => console.log('D'));console.log('E');A E C D B. Synchronous code runs to completion, then the microtask queue drains in FIFO order
(C, then D), and only then does the timer callback run as a task. If you got that right, the
rest of the section is detail. If not, start with the event loop.
Follow-up questions
Section titled “Follow-up questions”- Why is
setTimeout(fn, 0)not actually zero milliseconds? - What happens to the UI if a promise callback schedules another promise callback forever?
- If JavaScript is single-threaded, how does
fetchnot block? - Why does
constnot make an object immutable? - What is the difference between
==and===, and why does the language keep both?
Common traps
Section titled “Common traps”- “JavaScript is asynchronous.” It is synchronous with an asynchronous scheduling mechanism. The language itself never runs two statements at once.
- “Promises are faster than callbacks.” They are not faster; they are more composable and their errors propagate.
- “
varis function-scoped, so it is hoisted with its value.” It is hoisted and initialised toundefined; the assignment still happens in place. - “Classes are the object model.” The object model is prototypes.
classis syntax over it. - “Microtasks run on a separate thread.” They run on the same stack, immediately after the current task.