Quiz · Hard
Harder questions on bound this, prototype shadowing, class fields, microtask ordering, generators, iterators, key order and module behavior. Output questions were run to confirm the answers.
What does this code print?
function f() { return this.x; }
const a = { x: 1 }, b = { x: 2 };
const g = f.bind(a);
console.log(g.call(b), f.call(b));1 2
2 1
2 2
1 1
A bound function permanently fixes this; a later call cannot change it, so g.call(b) still uses a and returns 1. f.call(b) on the original function uses b and returns 2.
What does this code print?
class A {
m() {}
p = () => {};
}
const x = new A(), y = new A();
console.log(x.m === y.m, x.p === y.p);true false
false true
false false
true true
Methods defined in the class body live once on A.prototype, so all instances share the same function. A class field with an arrow function creates a new function per instance (handy for keeping this, but costs memory).
What does this code print?
function F() {}
F.prototype.v = 1;
const o = new F();
F.prototype = { v: 2 };
const p = new F();
console.log(o.v, p.v);1 2
1 1
2 1
2 2
An instance's prototype is fixed at creation. o still points to the old prototype object (v = 1). Re-assigning F.prototype only affects objects created afterwards.
What does this code print?
const proto = { n: 1 };
const o = Object.create(proto);
o.n++;
console.log(o.n, proto.n, Object.hasOwn(o, 'n'));2 1 true
2 2 false
1 1 false
2 1 false
o.n++ reads n through the prototype (1) but writes to o itself, creating an own property n = 2 that shadows the prototype's. The prototype object is unchanged.
What does this code print?
async function a() {
console.log('a1');
await b();
console.log('a2');
}
async function b() { console.log('b'); }
console.log('s');
a();
Promise.resolve().then(() => console.log('p'));
console.log('e');s e a1 b a2 p
s a1 b a2 e p
s a1 e b a2 p
s a1 b e a2 p
Everything before the first await runs synchronously (s, a1, b), then the script ends with e. Microtasks run in the order queued: the continuation of a (a2) was queued before the .then (p).
What does this code print?
Promise.resolve().then(() => console.log(1)).then(() => console.log(2)).then(() => console.log(3));
Promise.resolve().then(() => Promise.resolve()).then(() => console.log('A'));A 1 2 3
1 2 A 3
1 2 3 A
1 A 2 3
Returning a promise from a .then handler makes the outer promise wait for it, which costs extra microtask ticks. So A falls behind the plain chain, even though both started together.
What does this code print?
function f() {
try {
return 'try';
} finally {
console.log('finally');
}
}
console.log(f());try
try finally
finally
finally try
A finally block always runs, even after a return in try. It runs before the function actually returns, so 'finally' is logged first and then the returned value is printed.
What does this code print?
function* g() {
const x = yield 1;
console.log(x);
yield 2;
}
const it = g();
console.log(it.next().value, it.next('hi').value);1 hi 2
1 2
hi 1 2
hi 2 1
The argument of the second next('hi') becomes the result of the paused yield 1, so x is 'hi' and is logged while evaluating the arguments of the outer console.log, which then prints 1 and 2.
What does this code print?
const r = {
from: 1,
to: 3,
[Symbol.iterator]() {
let c = this.from, t = this.to;
return { next: () => (c <= t ? { value: c++, done: false } : { value: undefined, done: true }) };
}
};
console.log([...r]);[1, 2, 3]
[1, 3]
TypeError: r is not iterable
[]
Spread and for...of call obj[Symbol.iterator]() and then next() until done is true. Any object that implements the protocol is iterable.
What does this code print?
console.log(Array(3).map((_, i) => i), Array.from({ length: 3 }, (_, i) => i));[0, 1, 2] [0, 1, 2]
[undefined, undefined, undefined] [0, 1, 2]
[ <3 empty items> ] [0, 1, 2]
[] [0, 1, 2]
Array(3) creates three holes, and map skips holes, so nothing is mapped. Array.from({ length: 3 }, fn) visits every index.
What does this code print?
console.log([] == ![], null >= 0, null == 0);true false false
true true false
false true true
false false false
![] is false; [] == false converts both to 0 ('' to 0), so it is true. null >= 0 converts null to 0 for the relational comparison (true), but null == 0 is false because == only equates null with undefined.
What does this code print?
const o = {};
console.log(o.a?.b.c, o.f?.());undefined undefined
null undefined
undefined null
TypeError
Optional chaining short-circuits the whole rest of the chain: o.a?.b.c stops at a (undefined), so .c is never read. o.f?.() does not call f because it is undefined.
What does this code print?
console.log(Object.keys({ b: 1, 2: 1, a: 1, 1: 1 }));['b', '2', 'a', '1']
['1', '2', 'a', 'b']
[ '1', '2', 'b', 'a' ]
['b', 'a', '1', '2']
Own property order: integer-like keys first in ascending numeric order, then string keys in insertion order (then symbols). So 1, 2, then b, a.
Two modules both import { counter } from './state.js'. How many times does state.js execute, and are the imports shared?
Every time counter is accessed
Twice, then it is cached
Once; both get the same live bindings of the same module instance
Once per importing module; each gets its own copy
ES modules are singletons per URL: evaluated once and cached. Imports are live bindings to the exported variables, so changes made inside state.js are visible to all importers (importers cannot assign to them directly).
try { setTimeout(() => { throw new Error('x'); }, 0); } catch (e) { console.log('caught'); }: what happens?
The timer never runs
The error is silently ignored
The error is not caught; it surfaces later as an uncaught exception
'caught' is logged
The callback runs later as its own macrotask, after the try/catch has finished, so that call stack no longer exists. Handle errors inside the callback (or use promises with .catch).
A class declares #secret = 1. What happens with obj.#secret outside the class body?
A TypeError at runtime only
A SyntaxError at parse time
undefined
It works, but logs a warning
Private names are checked statically: referring to #secret outside the class is an early SyntaxError, not just undefined. Reading a private field on an object that lacks it inside the class throws a TypeError.
const f = obj.method; f(); fails because this is lost. Which fix does not work?
obj.method.call() with no arguments
obj.method() called directly
const f = () => obj.method()
const f = obj.method.bind(obj)
call() with no thisArg calls the function with this undefined (strict) or the global object, so it does not restore obj. bind(obj), an arrow wrapper and a direct member call all keep obj as this.