var vs let Scope Quiz
Question
What happens when you declare a variable with `let` inside an if block and try to access it outside?
Hint
let is block-scoped, unlike var which is function-scoped.
Explanation
let declarations are block-scoped — they only exist within the nearest enclosing { } block. Accessing a let variable outside its block throws a ReferenceError. var is function-scoped, so it would be accessible outside the if block (but not outside the function).
Related Resources
Related Lessons
- JavaScript Variables
Learn about var, let, and const scoping.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.