Question Bank

JavaScript Scope and Closure Code Traces

Difficulty: Medium

Six traces covering the `var` loop-closure pitfall, parameter shadowing, IIFE captures, block scope leaks, and `delete` on locals.

Question Bank
/

JavaScript Scope and Closure Code Traces

JavaScript Scope and Closure Code Traces

Six traces covering the `var` loop-closure pitfall, parameter shadowing, IIFE captures, block scope leaks, and `delete` on locals.

Question Bank
Medium
JavaScript
6 questions
quiz
interview-prep
closures
js-lexical-scope

1,192 views

11

What do these two calls print, and why do they not both log 5?

Examples

Example 1:

Input: arr[2](); arr[3]();
Output: 6; 7
Explanation: All five closures share the same function-scoped i, which is 5 after the loop, so each call increments the same i and logs the new value.