How to declare a block-scoped variable in JavaScript?

  • var keyword
  • const keyword
  • let keyword
  • block keyword
In JavaScript, you can declare a block-scoped variable using the let keyword. Variables declared with let are limited in scope to the block, statement, or expression in which they are defined. This helps prevent unintended variable hoisting and allows you to create variables with localized scope within functions or blocks. var is function-scoped, and const is used to declare constants. block is not a valid keyword for variable declaration.
Add your answer
Loading...

Leave a comment

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