Block Scope with let
Fill in the Blank Easy
Question
Fill in the blank so the variable is scoped to the if block only.
Code
if (true) {
___ message = "inside";
console.log(message);
}
// console.log(message); // ReferenceError
Hint
Both let and const are block-scoped, but var is not.
Explanation
let (and const) are block-scoped — they only exist within the nearest curly-brace block. var is function-scoped, so it would leak outside the if block. Using let message confines it to the if block, and referencing it outside throws a ReferenceError.
Related Resources
Related Lessons
- Scope and Visibility
Learn about scope and variable visibility.
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.