---
title: "var, let, and const in JavaScript."  
description: "var, let, and const in JavaScript."  
author: "Amrith Chandran"  
published: 2025-11-04  
updated: 2025-11-04  
canonical: https://www.mindstick.com/interview/34401/var-let-and-const-in-javascript  
category: "javascript"  
tags: ["javascript", "datatype"]  
reading_time: 4 minutes  

---

# var, let, and const in JavaScript.

Let's understand the differences between `var`, `let`, and `const` in JavaScript with the help of simple examples.

#### var

- Declared variables have **function scope** (not block scope).
- They can be **redeclared** and **updated**.
- Supports **hoisting** (moved to the top of the scope, initialized with `undefined`).

## Example-

```javascript
var name = "Manish";
console.log(name); // output- Manish

var name = "Sharma"; // Redeclare the name variable with new value
console.log(name); // output- Sharma

if (true) {
 var age = 30; // var ignores block level scope
}
console.log(age); // 30 (accessible outside block)
```

#### let

- Declared variables are **block-scoped** (only within the {}).
- They can be **updated**, but **not redeclared** in the same scope.
- They are **not hoisted** in the same way (they exist in a "temporal dead zone" until defined).

## Example-

```javascript
let city = "Delhi";
console.log(city); //output- Delhi
city = "Mumbai"; // can update or reasign a new value
console.log(city); //output- Mumbai

let city = "Noida"; // Return error: can't redeclared

if (true) {
 let country = "India";
 console.log(country); //output- India
}
// console.log(country); //Return error (not accessible outside block)
```

#### const

- Declared variables are **block-scoped** (only within the {}).
- It is mandatory to **initialize** it when **declaring** it.
- It cannot be **updated or redeclared**.
- For **objects/arrays**, you can modify their contents (but not **reassign** them).

## Example-

```javascript
const pi = 3.1416;
// pi = 3.14; // Error - cannot reassign
const user = { name: "Manish" };

user.name = "Sharma"; // allowed (object content can change)
console.log(user.name); // outout- Sharma

// user = { name: "New" }; // Error - cannot reassign object
```

Let's see a quick example to compare all.

```javascript
function demo() {
 var a = 10;
 let b = 20;
 const c = 30;
 if (true) {
   var a = 100; // same variable (function-scoped)
   let b = 200; // new variable (block-scoped)
   const c = 300; // new variable (block-scoped)
   console.log(a, b, c); // output- 100 200 300
 }
 console.log(a, b, c); // output- 100 20 30
}
demo();
```

Would you like to learn about: [What is a Service Worker, and how does it differ from a traditional JavaScript script?](https://www.mindstick.com/interview/34348/what-is-a-service-worker-and-how-does-it-differ-from-a-traditional-javascript-script)

## Answers

### Answer by Amrith Chandran

Let's understand the differences between `var`, `let`, and `const` in JavaScript with the help of simple examples.

#### var

- Declared variables have **function scope** (not block scope).
- They can be **redeclared** and **updated**.
- Supports **hoisting** (moved to the top of the scope, initialized with `undefined`).

## Example-

```javascript
var name = "Manish";
console.log(name); // output- Manish

var name = "Sharma"; // Redeclare the name variable with new value
console.log(name); // output- Sharma

if (true) {
 var age = 30; // var ignores block level scope
}
console.log(age); // 30 (accessible outside block)
```

#### let

- Declared variables are **block-scoped** (only within the {}).
- They can be **updated**, but **not redeclared** in the same scope.
- They are **not hoisted** in the same way (they exist in a "temporal dead zone" until defined).

## Example-

```javascript
let city = "Delhi";
console.log(city); //output- Delhi
city = "Mumbai"; // can update or reasign a new value
console.log(city); //output- Mumbai

let city = "Noida"; // Return error: can't redeclared

if (true) {
 let country = "India";
 console.log(country); //output- India
}
// console.log(country); //Return error (not accessible outside block)
```

#### const

- Declared variables are **block-scoped** (only within the {}).
- It is mandatory to **initialize** it when **declaring** it.
- It cannot be **updated or redeclared**.
- For **objects/arrays**, you can modify their contents (but not **reassign** them).

## Example-

```javascript
const pi = 3.1416;
// pi = 3.14; // Error - cannot reassign
const user = { name: "Manish" };

user.name = "Sharma"; // allowed (object content can change)
console.log(user.name); // outout- Sharma

// user = { name: "New" }; // Error - cannot reassign object
```

Let's see a quick example to compare all.

```javascript
function demo() {
 var a = 10;
 let b = 20;
 const c = 30;
 if (true) {
   var a = 100; // same variable (function-scoped)
   let b = 200; // new variable (block-scoped)
   const c = 300; // new variable (block-scoped)
   console.log(a, b, c); // output- 100 200 300
 }
 console.log(a, b, c); // output- 100 20 30
}
demo();
```

Would you like to learn about: [What is a Service Worker, and how does it differ from a traditional JavaScript script?](https://www.mindstick.com/interview/34348/what-is-a-service-worker-and-how-does-it-differ-from-a-traditional-javascript-script)


---

Original Source: https://www.mindstick.com/interview/34401/var-let-and-const-in-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
