
JavaScript Variable Scope and Hoisting
Variable Scope in JavaScript
Variable scope determines where a variable is accessible within a program. JavaScript supports two primary types of scope: global and local.
Global Scope
Variables declared outside of any function or block are in the global scope. They are accessible from anywhere in the code.
let globalVar = "I'm global";
function checkScope() {
console.log(globalVar); // Accessible
}
checkScope();Function Scope (var)
Variables declared with var are function-scoped, meaning they are accessible within the entire function, regardless of block structure.
function example() {
var functionVar = "I'm function-scoped";
if (true) {
var functionVar = "Reassigned inside block";
console.log(functionVar); // "Reassigned inside block"
}
console.log(functionVar); // "Reassigned inside block"
}Block Scope (let and const)
Variables declared with let and const are block-scoped, meaning they are only accessible within the block they are declared in.
function example() {
let blockVar = "I'm block-scoped";
if (true) {
let blockVar = "Reassigned inside block";
console.log(blockVar); // "Reassigned inside block"
}
console.log(blockVar); // "I'm block-scoped"
}| Declaration | Scope | Reassignment | Redeclaration |
|---|---|---|---|
var | Function | Yes | Yes |
let | Block | Yes | No |
const | Block | No | No |
Hoisting in JavaScript
Hoisting is the default behavior of JavaScript where variable and function declarations are moved to the top of their scope during the compilation phase. However, only the declarations are hoisted, not the assignments.
Hoisting with var
console.log(hoistedVar); // undefined
var hoistedVar = "I'm hoisted";Hoisting with let and const
console.log(hoistedLet); // ReferenceError: hoistedLet is not defined
let hoistedLet = "I'm hoisted";The let and const declarations are hoisted but not initialized, leading to a "temporal dead zone" where accessing them before declaration results in a ReferenceError.
Best Practices
- Prefer
letandconstovervarto avoid unexpected behavior from function scope and hoisting. - Use
constfor values that should not change to enforce immutability. - Avoid global variables to prevent naming conflicts and improve maintainability.
- Understand the temporal dead zone when using
letandconstto avoid runtime errors.
