Mastering JavaScript Closures and Scoping: High-Level Interview Questions Quiz

  1. Question 1: Loop Closure Puzzle

    Given the following code snippet, what will be logged to the console after 3 seconds?nnconst arr = [10, 12, 15, 21];nfor (var i = 0; i u003C arr.length; i++) {n setTimeout(function() {n console.log('Index: ' + i + ', element: ' + arr[i]);n }, 3000);n}

    1. A. Logs four lines: Index: 4, element: undefined
    2. B. Logs: Index: 0, element: 10; Index: 1, element: 12; Index: 2, element: 15; Index: 3, element: 21
    3. C. Throws a ReferenceError due to i not being defined inside setTimeout
    4. D. Logs four lines: Index: i, element: arr[i]
    5. E. Logs: Index: NaN, element: undefined
  2. Question 2: Fixing Scope in Loops

    Which modification ensures that the index i inside setTimeout refers to the current loop value for every iteration when using a for loop in JavaScript?

    1. A. Replace var with let for the loop index
    2. B. Use setInterval instead of setTimeout
    3. C. Move setTimeout outside the loop
    4. D. Declare i as a global variable
    5. E. Prefix setTimeout with async
  3. Question 3: Understanding Closures

    Why does the original code with var in the loop cause all setTimeout callbacks to log the same index after the loop, rather than each logging the expected index value?

    1. A. Because setTimeout executes immediately and captures i by value
    2. B. Because var is function-scoped, not block-scoped, leading all callbacks to reference the final value of i
    3. C. Because setTimeout loses its closure over i inside loops
    4. D. Because the event loop resets the i variable during execution
    5. E. Because i is captured by reference only in strict mode
  4. Question 4: Applying IIFE for Closure

    Consider using an immediately invoked function expression (IIFE) inside the for loop: for (var i = 0; i u003C arr.length; i++) { setTimeout(function(i_local) { return function() { console.log(i_local); }; }(i), 3000); }. What key effect does this pattern achieve?

    1. A. All callbacks share a single i_local, still printing the same value
    2. B. Each callback receives its own copy of the i value via the parameter, so logs the expected indices 0 to 3
    3. C. The function runs immediately and returns undefined
    4. D. It causes a syntax error due to double parentheses
    5. E. It delays loop execution until all timeouts finish
  5. Question 5: let vs var in Loop Scoping

    If you rewrite the loop header as for (let i = 0; i u003C arr.length; i++), why do setTimeout callbacks inside this loop log the correct corresponding index and element each time?

    1. A. Each iteration creates a new closure with a fresh binding of i due to let being block-scoped
    2. B. let variables are hoisted and shared, but retain old values automatically
    3. C. setTimeout binds the loop variable synchronously when let is used
    4. D. The event loop handles let differently than var for callbacks
    5. E. let internally converts the function into an arrow function