Interview Q&A · JavaScript · 12 questions in this topic
Tricky Outputs & Gotchas — JavaScript Interview Q&A
The prediction questions interviewers love — coercion, hoisting, this-loss and async ordering. Part of Tricky Outputs & Coding Tasks · 204 questions in the JavaScript bank.
What logs: `console.log(1 + "2" + 3)` vs `console.log(1 + 2 + "3")`?
Answer
Left-to-right with + stringifying on first string contact: 1 + '2' → '12', then + 3 → '123'. Versus 1 + 2 → 3 (numeric), then + '3' → '33'. Type flows left to right; the first string operand flips the rest to concat.
First-string-flips-the-chain is the evaluation rule.
Example
console.log(1 + '2' + 3); // '123' console.log(1 + 2 + '3'); // '33'
What logs: `var` loop with `setTimeout` (and the three fixes)?
Answer
3 3 3: one shared var binding read after the loop finishes. Fixes: let (per-iteration bindings), IIFE capture per iteration (pre-let), forEach/for...of with const per element.
The evergreen closure-scope interview question.
Example
for (var i = 0; i < 3; i++) setTimeout(() => console.log(i)); // 3 3 3 for (let j = 0; j < 3; j++) setTimeout(() => console.log(j)); // 0 1 2
💡 Shared-vs-per-iteration is the whole explanation.
What is `this` inside: method vs detached vs arrow vs callback?
Answer
Method call (o.m()): o. Detached (const f = o.m; f()): undefined/global. Arrow defined in method: lexical method this (safe). Bare callback (setTimeout(o.m)): detached (lost).
Detachment-loses-receiver explains every variant.
Example
const o = { n: 1, m() { return this?.n; } }; o.m(); // 1 (receiver) const f = o.m; f(); // undefined (detached, strict)
What logs: promise executor vs `.then` vs `setTimeout`?
Answer
Executor runs synchronously at construction; then-callbacks queue as microtasks; setTimeout queues a macrotask. Order: executor-body, sync-after, microtasks, macrotask.
Sync-executor is the surprise to lead with.
Example
console.log('a'); new Promise((res) => { console.log('b'); res(); }) .then(() => console.log('c')); setTimeout(() => console.log('d'), 0); console.log('e'); // a b e c d
💡 a-b-e-c-d is the canonical answer to recite with reasons.
What does `[] + []`, `[] + {}`, `{} + []` evaluate to (and why)?
Answer
[] + [] → '' (both join to empty, concat). [] + {} → '[object Object]' (array joins empty, object stringifies). {} + [] → 0 in statement position (parsed as block + array→0)! plus '[object Object]' in expression position.
ToPrimitive-plus-position-parsing explains the full zoo — and why nobody writes such code.
Example
[] + []; // '' (expression) [] + {}; // '[object Object]' // {} + []; // 0 as statement (block!), '[object Object]' as expression
💡 Statement-vs-expression parsing is the {} twist.
Why does `0.1 + 0.2 === 0.3` fail (and the fix)?
Answer
Binary floating point approximates decimals: 0.1 + 0.2 = 0.30000000000000004. Fix: integer cents, decimal libraries, or epsilon-tolerant comparison.
Never === floats from arithmetic.
Example
Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON; // true (tolerant)
What happens: `typeof null`, `null == undefined`, `null === undefined`?
Answer
typeof null → 'object' (legacy bug). null == undefined → true (loose pair). null === undefined → false (distinct types).
The intentional x == null idiom (both nullish) is the one legitimate == use.
Example
typeof null; // 'object' (wart) null == undefined; // true null === undefined; // false
💡 x == null as nullish-check is the sanctioned exception.
What logs with `async` functions: order of `await` vs sync code?
Answer
Async bodies run sync until the first await, then yield; continuations are microtasks. Caller code after the call runs before continuations.
Sync-to-first-await is the execution rule.
Example
async function f() { console.log('a'); await null; console.log('c'); } console.log('b'); f(); console.log('d'); // b a d c
💡 Trace b-a-d-c aloud to demonstrate the model.
Why does `[10, 9, 80].sort()` give `[10, 80, 9]`?
Answer
Default string (UTF-16) comparison. Numeric comparator (a, b) => a - b fixes it; toSorted preserves the original.
String-by-default is the sort gotcha.
Example
[10, 9, 80].sort((a, b) => a - b); // [9, 10, 80]
What is `NaN === NaN` (and reliable NaN checks)?
Answer
false — NaN is unequal to everything including itself. Number.isNaN (no coercion) detects it; Object.is(NaN, NaN) is true.
Self-inequality is the NaN signature.
Example
NaN === NaN; // false Number.isNaN(NaN); // true
What does `forEach` return, and can you `break`/`await` in it?
Answer
Returns undefined (never assign it); cannot break/continue (use for...of or some/every); async callbacks fire unawaited (use for...of with await or Promise.all(map)).
forEach is fire-and-forget side effects only.
Example
// for (const x of xs) { await f(x); } // awaited serial // await Promise.all(xs.map(f)); // concurrent
What logs: nested microtasks vs `setTimeout` chains?
Answer
Microtasks drain fully (including newly queued) before the next macrotask — nested .then chains all run before any setTimeout callback, however deeply nested.
Drain-fully-including-new is the starvation-capable rule.
Example
setTimeout(() => console.log('t'), 0); Promise.resolve() .then(() => console.log('m1')) .then(() => console.log('m2')); // m1 m2, then t
Recommended resourcesAffiliate links · may earn us a commission