In this tutorial we will learn about const, let and Block Scoping, before that we have posted tutorials about examples Arrow Functions, Destructuring, Rest Operators, Template Literal Classes & Inheritance, Modules promises and also Aync/Await. In this tutorial we will talk about const, let, and Block Scoping with code examples. See below how it works:
So, const and let provide block-scoped alternatives to var.
// const for constant values const PI = 3.14159;
// PI = 3.14; // This would throw an error
// let for variables that can be reassigned let count = 0;
count++; // This is fine
// Block scoping if (true) {
const x = 10; let y = 20;
var z = 30;
}
// console.log(x); // ReferenceError
// console.log(y); // ReferenceError
console.log(z); // 30 (var is function-scoped)
// Temporal Dead Zone (TDZ)
// console.log(a); // ReferenceError let a = 5;
Best Practices
- Use const by default, and let only when reassignment is necessary. Avoid using var in modern JavaScript code.
- Declare variables at the top of their scope for clarity.
Common Pitfalls
- Thinking const makes the value immutable (it doesn’t for objects and arrays). Accessing variables before their declaration (TDZ).
- Assuming block scope for var declarations.












