Quiz · Hard
Predict the console output of short JavaScript snippets covering type coercion, var vs let, hoisting, closures, the event loop, references and sorting. Common interview material.
What does this code print?
console.log(typeof null);null
object
undefined
number
typeof null returns the string "object". This is a long-standing quirk from the first version of JavaScript that cannot be fixed without breaking the web. To check for null, use value === null.
What does this code print?
console.log(1 + "2" - 1);"121"
2
11
NaN
Left to right: 1 + "2" concatenates to the string "12" because + prefers strings. Then - only works on numbers, so "12" - 1 converts to 12 − 1 = 11.
What does this code print?
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 0);
}3 3 3
0 1 2
0 0 0
undefined undefined undefined
var is function-scoped, so all three callbacks share one i. They run after the loop has finished, when i is 3.
What does this code print?
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 0);
}0 1 2
3 3 3
2 2 2
A ReferenceError
let creates a new binding for each loop iteration, so each callback closes over its own i. This is the standard fix for the var version.
What does this code print?
console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
console.log("D");A D C B
A B C D
A C D B
A D B C
Synchronous code runs first (A, D). Then the microtask queue (promise callbacks, C) is emptied before the next macrotask (the setTimeout callback, B), even with a 0 ms delay.
What does this code print?
console.log(x);
var x = 5;5
undefined
A ReferenceError
null
var declarations are hoisted and initialized to undefined; the assignment stays where it is. So x exists but has no value yet when it is logged.
What does this code print?
console.log(y);
let y = 5;A ReferenceError
undefined
5
null
let and const are hoisted too, but they stay uninitialized in the temporal dead zone until their declaration runs. Accessing them before that throws a ReferenceError.
What does this code print?
console.log(0 == "0", 0 === "0");true true
false false
false true
true false
== converts types before comparing, so "0" becomes 0. === also compares types, and number is not string. Prefer === to avoid surprises.
What does this code print?
function makeCounter() {
let count = 0;
return () => ++count;
}
const c = makeCounter();
c();
c();
console.log(c());1
0
3
2
The returned arrow function closes over count, which survives between calls. The three calls increment it to 1, 2, 3, and prefix ++ returns the new value.
What does this code print?
const user = { name: "Ana" };
user.name = "Ben";
console.log(user.name);Ana
Ben
A TypeError
undefined
const stops you from reassigning the variable, but the object it points to is still mutable. Use Object.freeze() to block property changes.
What does this code print?
const a = { x: 1, inner: { y: 2 } };
const b = { ...a };
b.x = 10;
b.inner.y = 20;
console.log(a.x, a.inner.y);1 20
1 2
10 20
10 2
Spread makes a shallow copy. x is copied by value, so a.x stays 1, but inner is copied by reference, so both objects share it. Use structuredClone(a) for a deep copy.
What does this code print?
console.log([10, 9, 1, 100].sort());[1, 10, 100, 9]
[1, 9, 10, 100]
[100, 10, 9, 1]
[10, 9, 1, 100]
Without a compare function, sort converts elements to strings and compares them character by character, so "100" comes before "9". Use .sort((a, b) => a - b) for numbers.
What does this code print?
console.log(typeof NaN, NaN === NaN);number false
NaN false
number true
undefined false
NaN is a special value of the number type, and it is the only value not equal to itself. Use Number.isNaN(value) to test for it.
What does this code print?
const { a = 5, b = 7 } = { a: undefined, b: null };
console.log(a, b);5 7
undefined null
5 null
undefined 7
Destructuring defaults apply only when the value is undefined. null counts as a real value, so b stays null. Default function parameters follow the same rule.