What is the difference between var, let, and const?
Suggested answer
var: Function-scoped (or global if outside a function). Hoisted to the top of its scope and initialised to undefined before code runs — you can reference a var before its declaration without a ReferenceError (you get undefined instead). Can be re-declared and re-assigned. let: Block-scoped (limited to the nearest {}). Hoisted but NOT initialised — accessing before declaration throws a ReferenceError (the Temporal Dead Zone). Cannot be re-declared in the same scope. Can be re-assigned. const: Block-scoped like let. Must be initialised at declaration. Cannot be re-assigned (the binding is constant), but the value it references can still be mutated (e.g., pushing to a const array is allowed). Best practice: use const by default; use let when re-assignment is needed; avoid var.
Practice content for interview preparation; not an official vendor answer. Verify details against current product documentation.
Community comments (0)
No comments yet.
Sign in or create a free account to add a comment. Comments are moderated before they appear.