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.
Global, Function, and Block Scope in JavaScript
Does var inside an if block leak to the outer scope in JavaScript?
The answer
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.
No. In ES modules, top-level let, const, and var are module-scoped, not global. They are only accessible outside the module if explicitly exported. This prevents global pollution.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.