How is block scope affected when using var compared to let and const?

  • var variables have block scope only within functions.
  • var variables have function scope.
  • let and const variables have block scope.
  • let and const variables have global scope.
Variables declared with "var" in JavaScript have function scope, meaning they are only scoped to the function in which they are declared. On the other hand, "let" and "const" introduce block scope, meaning they are scoped to the nearest enclosing block, like loops or conditionals. This block-level scoping behavior prevents issues like variable hoisting and improves code maintainability.
Add your answer
Loading...

Leave a comment

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