- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Block Scope
JavaScript Scope & Hoisting
JavaScript Block Scope
JavaScript block scope means
letandconstvariables only exist inside the braces where they were declared.
A block is any pair of braces: an if, a loop, or a plain pair on its own.
var ignores blocks entirely, which is the main reason it causes confusion.
Example
Example
javascript
if (true) {
let message = "inside";
console.log(message);
}
console.log(typeof message);The variable exists inside the block.
Outside, it is undefined, because it does not exist there at all.
