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

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.

```plaintext
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:

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

### ⚠️ Problems with `var`

#### ❌ Function Scope (Not Block Scope)

```plaintext
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

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

This can cause **bugs** in large projects.

---

#### ❌ Hoisting Confusion

```plaintext
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:

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

---

### ✅ Block Scope

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

`x` exists **only inside the block** `{}`.

---

### ❌ Cannot Be Re-declared

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

This helps avoid accidental mistakes.

---

### ⚠️ Hoisting (But Safer)

```plaintext
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:

```plaintext
const country = "India";
```

---

### ❌ Cannot Reassign

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

---

### ✅ Block Scoped (Like `let`)

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

---

### 🤔 const with Objects & Arrays

```plaintext
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

| Feature | var | let | const |
| --- | --- | --- | --- |
| Scope | Function | Block | Block |
| Re-declare | ✅ Yes | ❌ No | ❌ No |
| Re-assign | ✅ Yes | ✅ Yes | ❌ No |
| Hoisting | Yes (undefined) | TDZ | TDZ |
| Safe | ❌ No | ✅ Yes | ✅ Best |

---

## 🔹 Best Practice (Industry Rule)

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

```plaintext
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 👨‍💻🔥
