Which JavaScript keyword creates a block-scoped variable?

Study for the JavaScript Certification Test. Utilizes flashcards and multiple-choice questions; each question includes hints and explanations. Prepare to ace your exam!

The keyword that creates a block-scoped variable in JavaScript is "let." When you declare a variable using "let," it limits the scope of that variable to the nearest enclosing block, which can be a function or a loop, for example. This behavior is particularly useful for managing variable scope and avoiding accidental collisions with variables defined in outer scopes.

In contrast to "var," which declares a variable that can be accessed throughout the function in which it was declared regardless of block boundaries, "let" ensures that the variable is only available within the block in which it was defined.

"const" also creates a block-scoped variable, but it is specifically meant for declaring variables that should not be reassigned. While both "let" and "const" are block-scoped, "let" allows for reassignment, making it flexible for scenarios where variable values need to change.

Thus, "let" is the keyword that specifically aligns with the requirement of creating a block-scoped variable in JavaScript.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy