Topics
v6.0.0
On this page · sections

Top 30 Tricky JavaScript Output Questions — Panel Style

Module: JavaScript · Category: Tricky Output

30 panel-style questions grouped by concept — each snippet tests multiple ideas at once. Heavy focus on promises, async/await, and arrow vs regular function this binding.

Predict the full output, run in DevTools, then expand Answer & explanation.

TopicWeight
Event loop + Promises35%
Async/await20%
this binding20%
Closures + scope15%
Prototypes5%
Coercion5%

Coercion & equality

01. Coercion panel — +, ==, typeof, references

console.log([] + []); console.log([] + {}); console.log([] == false); console.log([] == ![]); console.log([1] == true); console.log(typeof null); console.log(null == undefined); console.log(null === undefined); console.log(2 + "2"); console.log(2 - "2"); console.log(0.1 + 0.2 === 0.3); console.log([] === []); console.log('b' + 'a' + +'a' + 'a'); console.log(1 < 2 < 3); console.log(3 > 2 > 1);

Covers: Array/object + coercion · Abstract equality == · typeof null · null vs undefined · String vs number + · Float precision · Reference equality · Unary + & chained comparisons

Answer & explanation

+: empty arrays stringify to ""; {} → "[object Object]". ==: [] → 0, false → 0; ![] is false; [1] → 1, true → 1. typeof null is the historic "object" bug. null == undefined is true; strict === is false. Mixed +: string concat vs numeric -. Floats: binary rounding makes 0.1 + 0.2 imprecise. References: two [] literals are different objects. Unary + on 'a' → NaN → "baNaNa". Chained <: left-associative — (1<2)<3 → true<3 → true; (3>2)>1 → true>1 → false.

02. Numbers panel — NaN, signed zero, truthy arrays

console.log(NaN === NaN); console.log(Object.is(NaN, NaN)); console.log(-0 === +0); console.log(Object.is(-0, +0)); console.log(Boolean([])); console.log(Number([])); console.log([10, 2].sort()); console.log([10, 2].sort((a, b) => a - b));

Covers: NaN equality · Object.is · Signed zero · Boolean/Number coercion

Answer & explanation

NaN: === is always false for NaN; Object.is(NaN, NaN) is true. Signed zero: === treats -0 and +0 as equal; Object.is distinguishes them. Arrays: [] is truthy; Number([]) → Number("") → 0. sort(): default lexicographic sort — "10" before "2"; numeric comparator fixes it.

03. Object Coercion + valueOf

const obj = { valueOf() { return 5; } }; console.log(obj + 1); console.log(obj == 5);

Covers: Coercion · valueOf

Answer & explanation

For obj + 1, valueOf() returns 5 → 5 + 1 === 6. For obj == 5, abstract equality coerces obj to 5 via valueOf() → 5 == 5 is true.

Hoisting, scope & prototypes

04. Hoisting & TDZ panel — var vs let

console.log("--- var ---"); console.log(a); var a = 1; console.log(a); console.log("--- let TDZ ---"); function outer() { console.log(x); let x = 2; } outer();

Covers: var hoisting · Temporal dead zone · Shadowing

Answer & explanation

var: declaration hoists and initializes to undefined before the assignment line runs. let: the inner x shadows any outer binding for the whole function body; reading x before let x = 2 runs throws ReferenceError (temporal dead zone) — not undefined.

05. TDZ + Closure

let x = 1; function outer() { console.log(x); let x = 2; return () => console.log(x); } outer();

Covers: TDZ · Closure · Scope

Answer & explanation

The inner let x shadows the outer x for the entire outer body. console.log(x) runs before initialization → temporal dead zone → ReferenceError. The returned closure is never reached.

06. Prototype + Shadowing

function Person() {} Person.prototype.name = "John"; const p = new Person(); p.name = "Mike"; delete p.name; console.log(p.name);

Covers: Prototype lookup · Shadowing · delete

Answer & explanation

p.name = "Mike" creates an own property that shadows Person.prototype.name. delete p.name removes the own property only. Lookup walks the prototype chain and finds "John".

07. Prototype Reassignment

function User(){} User.prototype.name = "A"; const u1 = new User(); User.prototype = { name: "B" }; const u2 = new User(); console.log(u1.name); console.log(u2.name);

Covers: Prototype references · Constructor behavior

Answer & explanation

new User() wires u1 to the current prototype object. Reassigning User.prototype does not retroactively change u1 — it still delegates to the old object ("A"). u2 is created after reassignment → "B".

Closures & loops

08. Loop + setTimeout — var vs let closures

for (var i = 0; i < 3; i++) { setTimeout(() => console.log("var", i), 0); } for (let j = 0; j < 3; j++) { setTimeout(() => console.log("let", j), 0); }

Covers: var shared binding · let per-iteration binding · setTimeout macrotask

Answer & explanation

var: one shared i; when macrotasks run, i === 3. let: each iteration gets its own j; each arrow callback captures that iteration’s value. Same arrow syntax — different scope rules.

09. Closure + let + Microtask

for (let i = 0; i < 3; i++) { Promise.resolve().then(() => { console.log(i); }); }

Covers: Block scope · Closure · Promise

Answer & explanation

Each let iteration creates a new binding. Every .then callback captures its own i. After sync code finishes, microtasks drain in order: 0, 1, 2.

10. Closure + var + Microtask

for (var i = 0; i < 3; i++) { Promise.resolve().then(() => { console.log(i); }); } console.log("done");

Covers: Closure · var · Promise · Microtask queue

Answer & explanation

var is function-scoped — one shared i. The loop finishes (i === 3) before any microtask runs. All three .then callbacks close over the same i, so each logs 3. "done" prints synchronously first.

Promises & event loop

11. Promise Chain + Error Recovery

Promise.resolve() .then(() => { throw "error"; }) .catch(() => { return 10; }) .then(console.log);

Covers: Rejection · Recovery · Promise chaining

Answer & explanation

Throwing in .then rejects the chain. .catch handles it and returns 10, which becomes a fulfilled value for the next .then. console.log receives 10.

12. Promise.then vs setTimeout(0)

console.log('1'); setTimeout(() => console.log('2'), 0); Promise.resolve().then(() => console.log('3')); console.log('4');
Answer & explanation

Sync code runs first. Microtasks (Promise callbacks) drain before the next macrotask (setTimeout).

Tip: event-loop, promises

13. Returning a Promise from .then (flattening)

Promise.resolve(1) .then((v) => { console.log("first", v); return Promise.resolve(v + 1); }) .then((v) => { console.log("second", v); });

Covers: Promise assimilation · Chaining · Microtasks

Answer & explanation

When a .then handler returns a Promise, the engine assimilates it — the outer chain waits for the inner Promise to settle and forwards its value. That is why the second .then receives 2, not a Promise object.

14. Nested .then inside a .then handler

Promise.resolve() .then(() => { console.log(1); Promise.resolve().then(() => console.log(2)); console.log(3); }) .then(() => console.log(4));

Covers: Microtask scheduling · Nested promises · Order of execution

Answer & explanation

The outer handler runs synchronously until it returns: logs 1, schedules inner 2, logs 3. The outer .then schedules 4. Microtasks drain FIFO: inner 2, then 4. Nested .then does not block the rest of the current handler.

15. .finally between .then handlers

Promise.resolve() .then(() => console.log("then")) .finally(() => console.log("finally")) .then(() => console.log("after"));

Covers: finally · Promise chaining · Microtask order

Answer & explanation

.finally runs after the preceding fulfillment and before the next .then. It does not swallow the value (unless it throws or returns a rejected Promise). All three callbacks are microtasks in registration order.

16. .catch that logs and re-throws

Promise.resolve() .then(() => { throw 1; }) .catch((e) => { console.log("catch", e); throw e; }) .catch((e) => console.log("final", e));

Covers: Rejection · .catch chaining · Error propagation

Answer & explanation

The first .catch handles the rejection, logs 1, then re-throws — the chain becomes rejected again. The second .catch handles that rejection and logs final 1.

17. Promise.all / race / interleaved chains

console.log("sync start"); Promise.resolve() .then(() => console.log("chain A")); Promise.resolve() .then(() => console.log("chain B")); Promise.all([Promise.resolve(1), Promise.resolve(2)]).then((v) => console.log("all", v) ); Promise.race([ Promise.resolve("winner"), Promise.resolve("loser"), ]).then((v) => console.log("race", v)); console.log("sync end");

Covers: Promise.all · Promise.race · Multiple chains · Microtask FIFO

Answer & explanation

Sync logs run first. Separate .then chains register microtasks in source order (A, then B). Promise.all resolves when all inputs settle → [1, 2]. Promise.race adopts the first settled result → "winner". All microtasks drain before the next macrotask.

18. Event Loop Nightmare

console.log(1); setTimeout(() => { console.log(2); Promise.resolve().then(() => { console.log(3); }); }); Promise.resolve().then(() => { console.log(4); setTimeout(() => { console.log(5); }); Promise.resolve().then(() => { console.log(6); }); }); console.log(7);

Covers: Macrotasks · Microtasks · Nested queueing

Answer & explanation

Sync: 1, 7. Microtasks drain fully: 4 (schedules macrotask 5 + microtask 6), then 6. First macrotask: 2 (schedules microtask 3), microtask 3. Second macrotask: 5. Rule: microtasks always empty before the next macrotask.

Async / await

19. Async/Await + Promise

async function foo() { console.log("A"); await Promise.resolve(); console.log("B"); } console.log("C"); foo(); Promise.resolve().then(() => { console.log("D"); }); console.log("E");

Covers: Async/await · Promise queue · Microtasks

Answer & explanation

Sync: C, then foo logs A and pauses at await (schedules resume microtask), then E. Microtasks: foo resumes with B, then the standalone D callback — FIFO order.

20. Async/Await + Nested Await

async function foo() { console.log(1); await Promise.resolve(); console.log(2); await Promise.resolve(); console.log(3); } foo(); Promise.resolve().then(() => { console.log(4); });

Covers: Multiple await · Queue ordering

Answer & explanation

Logs 1 synchronously. First await schedules resume → logs 2, second await schedules another resume. The .then(() => 4) is queued while foo is suspended. Microtasks run FIFO: resume (2), then 4, then final resume (3).

21. async function return value

async function f() { return 42; } const result = f(); console.log(result); console.log(result instanceof Promise);
Answer & explanation

Async functions always return a Promise. Use await f() or .then() to get 42.

Tip: async, promises

22. async/await logging order

async function main() { console.log('A'); await Promise.resolve(); console.log('B'); } console.log('C'); main(); console.log('D');
Answer & explanation

main() runs until await, then returns a pending Promise. Sync D runs. After sync stack, microtask resumes main at B.

Tip: async, event-loop

Arrow vs regular + this

23. Regular method vs arrow property on same object

const obj = { name: "JS", regular() { console.log("regular:", this.name); }, arrow: () => { console.log("arrow:", this.name); } }; obj.regular(); obj.arrow();

Covers: Method call this · Arrow lexical this · Object literals

Answer & explanation

regular() is invoked as obj.regular() → this is obj. The arrow is not a method with its own this; it closes over lexical this from the enclosing scope (module top-level → undefined in strict). Arrows are a poor choice for object “methods”.

24. this + Arrow + setTimeout

const obj = { name: "John", foo() { setTimeout(() => { console.log(this.name); }); } }; obj.foo();

Covers: this · Arrow functions · Async callbacks

Answer & explanation

Arrow functions do not have their own this — they inherit this from foo, which is obj when called as obj.foo(). The log runs in a later macrotask but still sees John.

25. this Lost Context

const obj = { name: "John", foo() { console.log(this.name); } }; const fn = obj.foo; fn();

Covers: Method extraction · this binding

Answer & explanation

Extracting obj.foo loses the receiver. A plain call fn() sets this to undefined in strict mode (ES modules are strict) → TypeError. In sloppy browser scripts, this is window and window.name is often "".

26. setTimeout — arrow vs regular this (side by side)

const obj = { name: "John", run() { setTimeout(() => { console.log("arrow:", this.name); }, 0); setTimeout(function () { console.log("regular:", this.name); }, 0); } }; obj.run();

Covers: Arrow this · Regular callback this · Macrotask

Answer & explanation

Both timers are scheduled from obj.run() where this === obj. The arrow callback keeps lexical this. The regular function is invoked by the timer host without a receiver → undefined in strict mode.

27. Promise .then — regular callback vs arrow

const obj = { name: "John", run() { Promise.resolve().then(function () { console.log("regular:", this.name); }); Promise.resolve().then(() => { console.log("arrow:", this.name); }); } }; obj.run();

Covers: this in Promise microtask · Regular fn this · Arrow capture

Answer & explanation

A regular .then callback is called as a plain function → this is undefined (strict). An arrow .then callback inherits this from run() → obj. Same queue, different this rules.

28. Explicit this — call, apply, and bind

function show() { console.log(this.id); } const a = { id: 1 }; const b = { id: 2 }; show.call(a); show.call(b); const bound = show.bind(a); bound(); bound.call(b);

Covers: call/apply · bind · Explicit this

Answer & explanation

call invokes with the given this (a, then b). bind(a) returns a new function with this permanently set to a — even bound.call(b) cannot override a bound this.

29. Async method + await preserves this

const obj = { name: "Amy", async run() { console.log("sync", this.name); await Promise.resolve(); console.log("after await", this.name); setTimeout(() => console.log("timeout arrow", this.name), 0); setTimeout(function () { console.log("timeout regular", this.name); }, 0); } }; obj.run();

Covers: Async methods · await · Arrow vs regular in async

Answer & explanation

An async method called as obj.run() keeps this === obj across await. After resume, this is still obj. Arrow setTimeout inherits it; regular setTimeout loses it (macrotask, own this).

30. Ultimate Combined Question

const obj = { name: "John", foo() { console.log(this.name); Promise.resolve().then(() => { console.log(this.name); }); setTimeout(() => { console.log(this.name); }); setTimeout(function() { console.log(this.name); }); return function() { console.log(this.name); }; } }; const fn = obj.foo(); fn();

Covers: this · Arrow functions · Promise · Event loop · setTimeout · Function invocation context

Answer & explanation

Sync: obj.foo() logs John. Returned fn(): plain call → this is undefined (strict: TypeError). Microtask (arrow): John. Arrow setTimeout: John. Function setTimeout: own this → undefined / global. This is the senior separator: trace sync vs microtask vs macrotask and arrow vs function this.

← JavaScript module · All A2Z tracks

Last updated on