Labs ICT
โญ Pro Login

Let & Const

1 min read | JavaScript Tutorial
โญ

Want the full learning experience?

Get structured courses, certificates, projects, and instructor support with LabsICT Pro.

Explore Pro Courses

let vs const vs var

Modern JavaScript gives you three ways to declare variables. Each serves a different purpose. Getting this right means fewer bugs and cleaner code.

  • const โ€” value cannot be reassigned. Use this by default.
  • let โ€” can be reassigned. Use when you need to update the value.
  • var โ€” old way. Avoid in modern code.

let vs const

let score = 10;
score = 20; // Works fine

const maxScore = 100;
maxScore = 200; // Error: Assignment to constant variable

const Does Not Mean Immutable

const person = { name: "Alice" };
person.name = "Bob"; // Works โ€” const prevents reassignment, not mutation
console.log(person);
// person = {}; // Error!

Block Scope vs Function Scope

if (true) {
  var a = "visible everywhere";
  let b = "only in this block";
}
console.log(a); // Works
console.log(b); // ReferenceError

Temporal Dead Zone (TDZ)

console.log(x); // undefined (var is hoisted)
var x = 5;

console.log(y); // ReferenceError: Cannot access before initialization
let y = 5;

let and const exist in a "temporal dead zone" from the start of the block until they're declared.

๐Ÿงช Quick Quiz

What is the difference between let and const?