Overview
Curated: · Written: · Reviewed:
JavaScript Fundamentals
In one line
JavaScript runs one call stack at a time, so closures, this, promises and the event loop all follow from two questions: when a binding is read, and when the stack reaches the queued work. Reach for this model when an interviewer asks why code prints what it prints.
Key definitions
- Reference: the value an object binding holds. Assignment and argument passing copy the reference, so a callee can mutate the object but cannot rebind the caller's variable.
- SameValueZero: the equality used by
includes,MapandSet. It matchesNaNtoNaNand treats0and-0as equal. - Abstract equality (
==): converts operands first, with objects going through ToPrimitive. It is not transitive. - Closure: a function plus the bindings it was created in. It reads those bindings at call time, not the values they held at creation.
- TDZ: the window in which a
letorconstbinding exists but is uninitialized. Reading it throws a ReferenceError. - Microtask: a promise reaction or
queueMicrotaskcallback. The microtask queue drains completely after each task.
Formulas and complexity
Safe integers run up to Number.MAX_SAFE_INTEGER:
Above that bound neighbouring integers collapse: rounds to (ties go to even). Quote this when asked about large identifiers. The fix is to carry them as strings and convert them with BigInt.
How it works
- A task runs to completion. Nothing interrupts it, including other callbacks.
- The microtask queue then drains fully, including microtasks queued by microtasks.
- In a browser the page may render before the next task starts.
awaitsuspends one function and schedules its resumption as a microtask. The loop keeps going.- A closure keeps every binding it reads reachable, so an un-removed listener holds its captures alive.
Trace this snippet (modern Node or browser):
console.log(1);
setTimeout(() => console.log(2), 0);
Promise.resolve().then(() => {
console.log(3);
queueMicrotask(() => console.log(4));
});
console.log(5);The script task prints 1, schedules the timer, schedules the promise reaction, then prints 5. The microtask queue drains next: the reaction prints 3 and queues a microtask, which the same drain runs and prints 4. Only then does the timer task print 2. Output: 1, 5, 3, 4, 2.
When to use and when not to
| Decision | Reach for | Avoid |
|---|---|---|
| Copy depth | structuredClone for real deep data | Spread on nested data. The JSON round trip drops undefined, turns Date into a string and throws on cycles |
| Equality | ===, and x == null to catch both null and undefined | == elsewhere. === never matches NaN, so use Number.isNaN |
| Concurrency | Promise.all for independent calls | await in a loop when the steps are independent |
| Function form | Arrows for callbacks inside a method | Arrows as object-literal methods, where this is the module scope |
| Loop closures | let per iteration | var with asynchronous callbacks |
Traps interviewers probe
- Saying objects are passed by reference. Mutation crosses the call; rebinding does not. Say the reference is passed by value.
- Parsing large IDs with
JSON.parse.9007199254740993is read back as9007199254740992, so distinct IDs collapse in aSet. Fix: send IDs as strings and convert them withBigInt. - Slicing strings by UTF-16 code unit.
name.slice(0, 8)can split a surrogate pair and render a replacement character. Fix: truncate by code point withArray.from(name), or by grapheme withIntl.Segmenter. - Using
||for defaults.0,''andfalseare replaced. Fix: use??when onlynullorundefinedshould mean absent. - Wrapping an un-awaited call in
try/catch. The catch never runs, and the rejection becomes unhandled, which ends a Node process by default. Fix:awaitthe call inside thetry.
Interview tips
Open with the model, then the answer: "This is a binding question. The value is read when the code runs, not when it was written."
Expect these follow-ups:
- Why does the
varloop print3, 3, 3, and what doesletchange? - Why is
thisundefinedafter a method is detached? (Module code is strict.) Why does a secondbinddo nothing? - Where does
Promise.alloverlap work, and what doesallSettledchange? - Why does a
forEachwith an async callback return before the rows arrive?
30-second outline:
- State the rule: bindings and receivers are resolved when the code executes.
- Trace one example, naming the binding each read touches.
- Give the fix and its cost: a per-iteration
let, a bound handler, an awaited loop, or a string ID.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.