Question 1
BeginnerEssential JavaScript Principles and Practices · Variables, Data Types, and Operators
A developer is refactoring a legacy JavaScript application to use modern ES6 syntax. The original code uses var for variable declaration inside a for loop, causing scope leakage. Which variable declaration keyword should be used to ensure the loop counter is block-scoped and not accessible outside the loop?
Answer and explanation
Correct answer: D
The let keyword declares a block-scoped local variable. Using let in a for loop ensures that the variable is only available within the loop block. var is function-scoped, and const cannot be used for a loop counter that needs to be incremented.
