StudyFox
कीमतेंब्लॉगएक्सटेंशनमुफ़्त टूल
  • कीमतें
  • ब्लॉग
  • एक्सटेंशन
  • मुफ़्त टूल
  • शुरू करेंसाइन इन
StudyFox

अपने नोट्स, PDF और स्लाइड्स को फ़्लैशकार्ड, क्विज़, नोट्स और माइंड मैप में बदलें।

StudyFox खोलें

स्टडी टूल्स

  • होमवर्क हेल्पर
  • नोट मेकर
  • फ़्लैशकार्ड मेकर
  • क्विज़ मेकर
  • माइंड मैप मेकर

प्रोडक्ट

  • कीमतें
  • Chrome एक्सटेंशन
  • मुफ़्त एक्सटेंशन
  • स्टडी सेट देखें
  • ब्लॉग

सहायता

  • सहायता केंद्र
  • संपर्क करें
  • गोपनीयता नीति
  • सेवा की शर्तें

तुलना करें

  • Chegg का विकल्प
  • Course Hero का विकल्प
  • Coursology का विकल्प
  • Photomath का विकल्प
  • Gauthmath का विकल्प
  • PhotoSolve का विकल्प
  • CollegeTools का विकल्प
  • Transcript का विकल्प
  • SmartSolve का विकल्प
  • Homeworkify का विकल्प
  • AnswersAi का विकल्प
  • StudyX का विकल्प
  • Quizlet का विकल्प
  • Anki का विकल्प
  • Knowt का विकल्प
  • Brainscape का विकल्प
  • सभी तुलनाएँ

© 2026 StudyFox AI. सर्वाधिकार सुरक्षित।

  • X
  • Discord
  • Instagram
ExploreEngineering & TechWeb DevelopmentJavaScript: Tricky Output Questions (Closures, Hoisting, Event Loop)

Quiz · Hard

JavaScript: Tricky Output Questions (Closures, Hoisting, Event Loop)

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.

by Kill h.14 questionsCreated Nov 8, 2026, 08:04 PM
Web Development· JavaScriptTags#javascript#interview-prep#closures#event-loop

14 questions

  1. 1

    What does this code print?

    console.log(typeof null);
    • A

      null

    • object

    • C

      undefined

    • D

      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.

  2. 2

    What does this code print?

    console.log(1 + "2" - 1);
    • A

      "121"

    • B

      2

    • 11

    • D

      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.

  3. 3

    What does this code print?

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

    • B

      0 1 2

    • C

      0 0 0

    • D

      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.

  4. 4

    What does this code print?

    for (let i = 0; i < 3; i++) {
      setTimeout(() => console.log(i), 0);
    }
    • 0 1 2

    • B

      3 3 3

    • C

      2 2 2

    • D

      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.

  5. 5

    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

    • B

      A B C D

    • C

      A C D B

    • D

      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.

  6. 6

    What does this code print?

    console.log(x);
    var x = 5;
    • A

      5

    • undefined

    • C

      A ReferenceError

    • D

      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.

  7. 7

    What does this code print?

    console.log(y);
    let y = 5;
    • A ReferenceError

    • B

      undefined

    • C

      5

    • D

      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.

  8. 8

    What does this code print?

    console.log(0 == "0", 0 === "0");
    • A

      true true

    • B

      false false

    • C

      false true

    • true false

    == converts types before comparing, so "0" becomes 0. === also compares types, and number is not string. Prefer === to avoid surprises.

  9. 9

    What does this code print?

    function makeCounter() {
      let count = 0;
      return () => ++count;
    }
    const c = makeCounter();
    c();
    c();
    console.log(c());
    • A

      1

    • B

      0

    • 3

    • D

      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.

  10. 10

    What does this code print?

    const user = { name: "Ana" };
    user.name = "Ben";
    console.log(user.name);
    • A

      Ana

    • Ben

    • C

      A TypeError

    • D

      undefined

    const stops you from reassigning the variable, but the object it points to is still mutable. Use Object.freeze() to block property changes.

  11. 11

    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

    • B

      1 2

    • C

      10 20

    • D

      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.

  12. 12

    What does this code print?

    console.log([10, 9, 1, 100].sort());
    • [1, 10, 100, 9]

    • B

      [1, 9, 10, 100]

    • C

      [100, 10, 9, 1]

    • D

      [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.

  13. 13

    What does this code print?

    console.log(typeof NaN, NaN === NaN);
    • number false

    • B

      NaN false

    • C

      number true

    • D

      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.

  14. 14

    What does this code print?

    const { a = 5, b = 7 } = { a: undefined, b: null };
    console.log(a, b);
    • A

      5 7

    • B

      undefined null

    • 5 null

    • D

      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.