Which statement about var, let, and const in JavaScript is true?

Prepare for the TJR Bootcamp Test with targeted questions and detailed explanations. Use mock exams to enhance understanding and boost your confidence. Gear up for success!

Multiple Choice

Which statement about var, let, and const in JavaScript is true?

Explanation:
Understanding how those three declarations work hinges on scope and whether a binding can be reassigned. Var is function-scoped, so it belongs to the function it’s in, and it can be redeclared within the same scope. Let and const are block-scoped, meaning they follow the boundaries of blocks like those in if statements or loops. Let bindings are mutable, so you can reassign them. Const bindings are not reassigned after their initial value is set, though if the bound value is an object, its properties can still be changed. That combination of rules is why the statement that var is function-scoped and can be redeclared, while let and const are block-scoped with let being mutable and const being read-only bindings, is the best description. The other options mix up scope or mutability: var being block-scoped and not redeclarable, let being function-scoped and immutable, or const being redeclarable.

Understanding how those three declarations work hinges on scope and whether a binding can be reassigned. Var is function-scoped, so it belongs to the function it’s in, and it can be redeclared within the same scope. Let and const are block-scoped, meaning they follow the boundaries of blocks like those in if statements or loops. Let bindings are mutable, so you can reassign them. Const bindings are not reassigned after their initial value is set, though if the bound value is an object, its properties can still be changed.

That combination of rules is why the statement that var is function-scoped and can be redeclared, while let and const are block-scoped with let being mutable and const being read-only bindings, is the best description. The other options mix up scope or mutability: var being block-scoped and not redeclarable, let being function-scoped and immutable, or const being redeclarable.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy