JavaScript practice questions with answers
Practise JavaScript questions at your own pace: types, equality, closures, this, promises and the event loop. Pick an answer to see whether you are right and why. Nothing is timed or ranked here; for that, play the Daily Quiz at 4 PM IST.
What does this print?
easy
console.log(typeof null);- "null"
- "object"
- "undefined"
- "number"
Answer: B. "object"
Explanation: A long-standing quirk of JavaScript: typeof null is "object". Check for null with value === null.
What does this print?
easy
console.log(1 + "2", 1 - "2");- 3 -1
- "12" -1
- 12 -1
- 3 "1-2"
Answer: C. 12 -1
Explanation: + with a string concatenates ("12"). - only works on numbers, so "2" is converted and 1 - 2 = -1.
Which keyword declares a block-scoped variable that cannot be reassigned?
easy
- var
- let
- const
- static
Answer: C. const
Explanation: const and let are block-scoped; const cannot be reassigned (but an object it holds can still be mutated). var is function-scoped.
What does [1, 2, 3].map(x => x * 2) return?
easy
- [2, 4, 6]
- [1, 2, 3]
- 12
- undefined
Answer: A. [2, 4, 6]
Explanation: map returns a new array with the function applied to each element; the original array is unchanged.
What does this print?
medium
console.log(0.1 + 0.2 === 0.3, [] == false, null == undefined);- false true true
- true false false
- false false true
- false true false
Answer: A. false true true
Explanation: Floating-point rounding makes the first false. Loose equality converts [] to "" to 0 and false to 0, so true; null == undefined is true by definition. Prefer ===.
What does this print?
medium
for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 0); }- 0 1 2
- 3 3 3
- 2 2 2
- undefined ×3
Answer: B. 3 3 3
Explanation: var is shared by the whole function, and the callbacks run after the loop has finished, when i is 3. With let each iteration gets its own i and prints 0 1 2.
What does this print?
medium
const a = { n: 1 }; const b = { ...a }; b.n = 2; console.log(a.n);- 1
- 2
- undefined
- TypeError
Answer: A. 1
Explanation: Spread makes a new (shallow) copy, so changing b.n does not affect a. Nested objects would still be shared.
In what order are the lines printed?
medium
console.log("A"); setTimeout(() => console.log("B"), 0); Promise.resolve().then(() => console.log("C")); console.log("D");- A B C D
- A D C B
- A D B C
- A C D B
Answer: B. A D C B
Explanation: Synchronous code runs first (A, D). Promise callbacks are microtasks and run before timer callbacks (macrotasks), so C comes before B.
What does this print?
hard
function counter() { let n = 0; return () => ++n; } const c1 = counter(), c2 = counter(); c1(); c1(); console.log(c1(), c2());- 3 1
- 3 3
- 1 1
- 2 1
Answer: A. 3 1
Explanation: Each call to counter creates a new closure with its own n. c1 has been called twice already, so it returns 3; c2 starts fresh at 1.
What does this print?
hard
// runs as an ES module const obj = { name: "H4H", regular() { return this.name; }, arrow: () => typeof this, }; console.log(obj.regular(), obj.arrow());- H4H object
- H4H undefined
- undefined undefined
- H4H string
Answer: B. H4H undefined
Explanation: Arrow functions do not have their own this; they use the surrounding scope's. At the top level of an ES module this is undefined.
What does Promise.all do if one of the promises rejects?
hard
- Waits for all, then resolves with the successful results
- Rejects immediately with that reason
- Ignores the rejected promise
- Retries it
Answer: B. Rejects immediately with that reason
Explanation: Promise.all fails fast. Use Promise.allSettled to wait for every result, success or failure.
Which statements are true? (Select all that apply)
hard
- Objects and arrays are passed by sharing a reference
- Strings are immutable
- NaN === NaN is true
- Array.prototype.sort sorts numbers numerically by default
Answer: A. Objects and arrays are passed by sharing a reference; B. Strings are immutable
Explanation: NaN is not equal to anything, itself included (use Number.isNaN). sort() compares as strings by default, so [10, 9, 1].sort() gives [1, 10, 9]; pass (a, b) => a - b.