var is function-scoped and can be redeclared, let is block-scoped and can be reassigned, const is block-scoped and cannot be reassigned after initialization.
JavaScript has three ways to declare variables:
var:
let:
const:
function scopeExample() {
if (true) {
var x = 'var';
let y = 'let';
const z = 'const';
}
console.log(x); // 'var' - accessible (function-scoped)
console.log(y); // ReferenceError - not accessible (block-scoped)
console.log(z); // ReferenceError - not accessible (block-scoped)
}Using const for API URLs, environment settings, and configuration values that should never change during runtime
Using let in for loops to maintain proper block scope and avoid closure issues with asynchronous callbacks
Using const for imported modules and dependencies to prevent accidental reassignment
Build an interactive tool that highlights variable accessibility across different scopes (global, function, block)
Create an interactive demonstration showing TDZ behavior with let and const declarations