Difference Between var let and const in JavaScript (With Examples 2026)

By Amarjeet Ram

Published on:

Difference Between var let and const in JavaScript

JavaScript gives developers three keywords to declare variables: var, let, and const. At first glance, they may look similar, but each behaves differently in terms of scope, hoisting, re-declaration, and re-assignment.
Understanding these differences is crucial for writing clean, bug-free, and modern JavaScript.

This article explains each keyword in detail with easy examples, tables, and best-practice guidance.

Also Read This

Types of Cables in Networking (2026): Complete Guide with Examples & Uses

1. What is var in JavaScript?

Difference Between var let and const in JavaScript

var is the oldest way of declaring variables in JavaScript. It was used before ES6 (2015) introduced let and const.

Key Features of var

  • Function-scoped
  • Can be re-declared
  • Can be updated
  • Hoisted (but initialized with undefined)
  • Not recommended in modern JS

Example of var


var name = "Amarjeet";
console.log(name);  // Amarjeet

var name = "Ram";   // Re-declared allowed
console.log(name);  // Ram

2. What is let in JavaScript?

let is the modern and recommended way to declare variables that can change later.

Key Features of let

  • Block-scoped
  • Cannot be re-declared in the same block
  • Can be updated
  • Hoisted (but not initialized → Temporal Dead Zone)

Example of let


let age = 18;
age = 19;  // Allowed
console.log(age); // 19

// let age = 20; ❌ Error: re-declaration not allowed

3. What is const in JavaScript?

const is used to declare variables that cannot be changed after assigning the initial value.

Key Features of const

  • Block-scoped
  • Cannot be re-declared
  • Cannot be updated
  • Must be initialized at declaration
  • Reference value can change (in objects/arrays)

Example of const


const country = "India";
console.log(country); // India

// country = "USA"; ❌ Error: Assignment not allowed

Important Note

For arrays and objects, the reference cannot change, but the contents can change.

Example:


const person = { name: "Avi" };
person.name = "Amar"; // Allowed
console.log(person.name); // Amar

4. Comparison Table: var vs let vs const

Featurevarletconst
ScopeFunctionBlockBlock
Re-declaration✔ Allowed❌ Not allowed❌ Not allowed
Re-assignment✔ Allowed✔ Allowed❌ Not allowed
Hoisting✔ Yes (initialized as undefined)✔ Yes (but TDZ)✔ Yes (but TDZ)
Best Use CaseLegacy codeValues that may changeConstant values, objects, arrays

5. Scope Difference Explained (Example)

var = function scope


if (true) {
  var x = 10;
}
console.log(x); // 10 (still accessible)

let / const = block scope


if (true) {
  let y = 20;
  const z = 30;
}
console.log(y); // ❌ Error
console.log(z); // ❌ Error


6. Hoisting Difference

var


console.log(a); // undefined
var a = 10;

let / const


console.log(b); // ❌ Error: Cannot access before initialization
let b = 20;

7. When Should You Use var, let, and const?

✔ Use const

  • For values that must not change
  • Best for objects, arrays, constants

✔ Use let

  • When the variable value will change (loops, counters, conditions)

❌ Avoid var

  • Causes bugs due to function scope and hoisting
  • Only use if working on old JS code

8. Real-Life Example Combining All


var siteName = "SmartToolsWala"; // Old style - avoid

let viewsToday = 1200;           // Value may change
viewsToday = 1500;

const owner = "Amarjeet";        // Constant

Also Read This

Top 10 Hidden HTML Tags Every Beginner Must Know (2026 Guide)

FAQs

1. Which one is best: var, let, or const?

const is best for most cases.
Use let only when you really need to change a value.

2. Why avoid var?

Because it is function-scoped, allows re-declaration, and may create unexpected bugs.

3. Can const variables change?

The variable cannot change, but objects or arrays inside them can be modified.

4. Is let faster than var?

Performance difference is negligible, but let and const are more predictable and modern.

5. Should I always use const?

Prefer const by default.
Switch to let only when needed.

Conclusion

Understanding the differences between var, let, and const makes your JavaScript code safer, cleaner, and more modern.
Use var only in old codebases, rely on let for changing values, and prefer const for constants and stable data.

Leave a Comment