Skip to main content

Command Palette

Search for a command to run...

var, let, and const in JavaScript – Explained with Simple Examples

Published
4 min readView as Markdown
T

I am a Computer Science graduate learning backend development with Node.js. I enjoy writing beginner-friendly articles and sharing what I learn along my journey. Currently focused on JavaScript, Node.js, and building real-world projects.

If you are learning JavaScript, one of the most confusing topics for beginners is:

❓ What is the difference between var, let, and const?

They are used to declare variables, but they behave very differently.

Let’s break it down in the simplest way possible 👇


🔹 What is a Variable?

A variable is a container used to store data.

name = "Tushar";

In JavaScript, we declare variables using:

  • var (old way)

  • let (modern)

  • const (modern & safe)


🔹 1️⃣ var – The Old Way (Avoid Using)

var was used before ES6 (2015).

Example:

var city = "Delhi";
console.log(city);

⚠️ Problems with var

❌ Function Scope (Not Block Scope)

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

Even though x it is inside if, it is still accessible outside.


❌ Can Be Re-declared

var name = "Tushar";
var name = "Kapil";
console.log(name); // Kapil

This can cause bugs in large projects.


❌ Hoisting Confusion

console.log(a);
var a = 5;

JavaScript moves var a to the top and sets it as undefined.


👉 Verdict on var

❌ Avoid using var
❌ Not safe for modern applications


🔹 2️⃣ let – The Modern Choice

let was introduced in ES6 and fixes most problems of var.

Example:

let age = 22;
age = 23; // allowed

✅ Block Scope

if (true) {
  let x = 10;
}
console.log(x); // ❌ Error

x exists only inside the block {}.


❌ Cannot Be Re-declared

let user = "Tushar";
let user = "Kapil"; // ❌ Error

This helps avoid accidental mistakes.


⚠️ Hoisting (But Safer)

console.log(a);
let a = 10; // ❌ Error

let is hoisted but stays in the Temporal Dead Zone until declared.


👉 When to Use let

✅ When will change
✅ Loop counters
✅ Conditional variables


🔹 3️⃣ const – Best & Safest Option

const is used for constant values.

Example:

const country = "India";

❌ Cannot Reassign

const pi = 3.14;
pi = 3.15; // ❌ Error

✅ Block Scoped (Like let)

if (true) {
  const x = 5;
}
console.log(x); // ❌ Error

🤔 const with Objects & Arrays

const user = {
  name: "Tushar"
};

user.name = "Kapil"; // ✅ Allowed

Why?

  • You cannot change the reference

  • But you can modify properties


👉 When to Use const

✅ Default choice
✅ Values that should not change
✅ Objects, arrays, functions


🔹 🔥 Key Differences Table

Featurevarletconst
ScopeFunctionBlockBlock
Re-declare✅ Yes❌ No❌ No
Re-assign✅ Yes✅ Yes❌ No
HoistingYes (undefined)TDZTDZ
Safe❌ No✅ Yes✅ Best

🔹 Best Practice (Industry Rule)

Always use const by default
Use let only when value needs to change
Never use var

const appName = "My App";
let counter = 0;
counter++;

🔹 Common Interview Question ❓

Q: Which one should I use most?
👉 const

Q: Is let better than var?
👉 Yes, always.

Q: Is var it used today?
👉 Rarely, only in old codebases.


🔹 Final Thoughts

Understanding var, let, and const is a core JavaScript concept.

If you master this:

  • Your code becomes cleaner

  • Bugs reduce

  • Interviews become easier

Small concepts make big developers 🚀


🙌 Thanks for Reading!

If this helped you:

  • Like ❤️

  • Share 🔁

  • Comment 💬

More JavaScript blogs coming soon 👨‍💻🔥