Skip to content

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.

“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.”

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
  1. Ordering. Predict the output of a snippet mixing setTimeout, Promise.then and synchronous logs. This is the single most common JavaScript screen question.
  2. Closures. Write a counter, a once wrapper, a memoiser, or explain why three handlers log the same index.
  3. this. Explain why a method passed as a callback loses its receiver.
  4. Promises. Difference between all, allSettled, race and any; sequential versus parallel awaits.
  5. Immutability and reference semantics. Why [...arr] is not a deep copy, why === on objects compares identity.
  6. The DOM and events. Delegation, bubbling, preventDefault versus stopPropagation.

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.

Predict the output before reading the answer.

ordering.js
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.

  1. Why is setTimeout(fn, 0) not actually zero milliseconds?
  2. What happens to the UI if a promise callback schedules another promise callback forever?
  3. If JavaScript is single-threaded, how does fetch not block?
  4. Why does const not make an object immutable?
  5. What is the difference between == and ===, and why does the language keep both?
  • “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.
  • “var is function-scoped, so it is hoisted with its value.” It is hoisted and initialised to undefined; the assignment still happens in place.
  • “Classes are the object model.” The object model is prototypes. class is syntax over it.
  • “Microtasks run on a separate thread.” They run on the same stack, immediately after the current task.