Which variable declaration should you use by default in modern JavaScript?
const by default, let when you need to reassign, and avoid var. Keep variables in the tightest scope possible to reduce bugs and improve readability.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Global, Function, and Block Scope in JavaScript
Global scope (variables accessible everywhere), function scope (variables accessible only inside a function), and block scope (variables accessible only inside a pair of braces).
var is function-scoped and leaks out of blocks; in the global scope it becomes a property of window. let and const are block-scoped and stay inside blocks; in the global scope they live in a separate global lexical environment.
Yes. var ignores block boundaries (except function bodies), so a var inside an if is hoisted to the enclosing function or global scope. let and const are block-scoped and do not leak.
Still have questions?
Browse all our FAQs or reach out to our support team
