You're reviewing a pull request, and you see that a developer used var to declare a variable inside a for loop. You notice that the variable is being accessed outside the loop without any issues. Why is this possible?

  • Variable Hoisting
  • Block Scoping (let/const)
  • Function Scoping
  • Global Scope
This is possible due to "Variable Hoisting" in JavaScript. Variables declared with var are hoisted to the top of their containing function or global scope. This means that the variable is accessible anywhere within that scope, even before its actual declaration in the code.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *