Hack4Hack

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.

  1. What does this print?

    easy

    console.log(typeof null);
    1. "null"
    2. "object"
    3. "undefined"
    4. "number"

    Answer: B. "object"

    Explanation: A long-standing quirk of JavaScript: typeof null is "object". Check for null with value === null.

  2. What does this print?

    easy

    console.log(1 + "2", 1 - "2");
    1. 3 -1
    2. "12" -1
    3. 12 -1
    4. 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.

  3. Which keyword declares a block-scoped variable that cannot be reassigned?

    easy

    1. var
    2. let
    3. const
    4. 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.

  4. What does [1, 2, 3].map(x => x * 2) return?

    easy

    1. [2, 4, 6]
    2. [1, 2, 3]
    3. 12
    4. undefined

    Answer: A. [2, 4, 6]

    Explanation: map returns a new array with the function applied to each element; the original array is unchanged.

  5. What does this print?

    medium

    console.log(0.1 + 0.2 === 0.3, [] == false, null == undefined);
    1. false true true
    2. true false false
    3. false false true
    4. 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 ===.

  6. What does this print?

    medium

    for (var i = 0; i < 3; i++) {
      setTimeout(() => console.log(i), 0);
    }
    1. 0 1 2
    2. 3 3 3
    3. 2 2 2
    4. 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.

  7. What does this print?

    medium

    const a = { n: 1 };
    const b = { ...a };
    b.n = 2;
    console.log(a.n);
    1. 1
    2. 2
    3. undefined
    4. 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.

  8. 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");
    1. A B C D
    2. A D C B
    3. A D B C
    4. 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.

  9. What does this print?

    hard

    function counter() {
      let n = 0;
      return () => ++n;
    }
    const c1 = counter(), c2 = counter();
    c1(); c1();
    console.log(c1(), c2());
    1. 3 1
    2. 3 3
    3. 1 1
    4. 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.

  10. 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());
    1. H4H object
    2. H4H undefined
    3. undefined undefined
    4. 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.

  11. What does Promise.all do if one of the promises rejects?

    hard

    1. Waits for all, then resolves with the successful results
    2. Rejects immediately with that reason
    3. Ignores the rejected promise
    4. 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.

  12. Which statements are true? (Select all that apply)

    hard

    1. Objects and arrays are passed by sharing a reference
    2. Strings are immutable
    3. NaN === NaN is true
    4. 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.

Play the Daily Quiz at 4 PM IST