Skip to content
Tech Interview Prep
Technical interview guide

JavaScript Fundamentals

Values and references, the four equality algorithms, scope and closures, this and arrow functions, prototypes and classes, the event loop, and modules.

Minutes to read
3
Practice MCQs
25
Interview Q&A
25
Edition
v3
Editorial status
Reviewed

Scope: ECMAScript 2024 and later, with Node behaviour cited from the active Node.js LTS line and browser behaviour from the WHATWG HTML event loop.

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, Map and Set. It matches NaN to NaN and treats 0 and -0 as 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 let or const binding exists but is uninitialized. Reading it throws a ReferenceError.
  • Microtask: a promise reaction or queueMicrotask callback. The microtask queue drains completely after each task.

Formulas and complexity

Safe integers run up to Number.MAX_SAFE_INTEGER:

253−1=90071992547409912^{53}-1 = 9007199254740991

Above that bound neighbouring integers collapse: 253+12^{53}+1 rounds to 2532^{53} (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.
  • await suspends 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):

JavaScript
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.

yes no Run one task to completion Drain microtask queue fully Browser and frame due? Render Take next task

When to use and when not to

DecisionReach forAvoid
Copy depthstructuredClone for real deep dataSpread 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
ConcurrencyPromise.all for independent callsawait in a loop when the steps are independent
Function formArrows for callbacks inside a methodArrows as object-literal methods, where this is the module scope
Loop closureslet per iterationvar with asynchronous callbacks

Traps interviewers probe

  1. Saying objects are passed by reference. Mutation crosses the call; rebinding does not. Say the reference is passed by value.
  2. Parsing large IDs with JSON.parse. 9007199254740993 is read back as 9007199254740992, so distinct IDs collapse in a Set. Fix: send IDs as strings and convert them with BigInt.
  3. 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 with Array.from(name), or by grapheme with Intl.Segmenter.
  4. Using || for defaults. 0, '' and false are replaced. Fix: use ?? when only null or undefined should mean absent.
  5. 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: await the call inside the try.

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 var loop print 3, 3, 3, and what does let change?
  • Why is this undefined after a method is detached? (Module code is strict.) Why does a second bind do nothing?
  • Where does Promise.all overlap work, and what does allSettled change?
  • Why does a forEach with an async callback return before the rows arrive?

30-second outline:

  1. State the rule: bindings and receivers are resolved when the code executes.
  2. Trace one example, naming the binding each read touches.
  3. 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.