---
title: "What is the difference between let, const, and var when declaring variables in JavaScript?"  
description: "What is the difference between let, const, and var when declaring variables in JavaScript?"  
author: "Ravi Vishwakarma"  
published: 2024-06-17  
updated: 2024-06-17  
canonical: https://www.mindstick.com/interview/33906/what-is-the-difference-between-let-const-and-var-when-declaring-variables-in-javascript  
category: "javascript"  
tags: ["web development", "javascript", "front-end development"]  
reading_time: 3 minutes  

---

# What is the difference between let, const, and var when declaring variables in JavaScript?

`let`, `const`, and `var` keywords are used for variable declarations, but they have some key differences in terms of scope, hoisting, and mutability

#### var:

- **Function-scoped or globally scoped**: Variables declared with `var` are function-scoped when declared inside a function, or globally scoped when declared outside any function.
- **Hoisting**: Variables declared with `var` are hoisted to the top of their scope and initialized with `undefined`. This means you can use a `var` variable before it's declared in the code.
- **Reassignment and redeclaration**: Variables declared with `var` can be redeclared and reassigned.

#### let:

- **Block-scoped**: Variables declared with `let` have block scope, which means they are only accessible within the nearest enclosing curly braces `{ }`.
- **Hoisting**: Variables declared with `let` are hoisted to the top of their block but are not initialized. This means you cannot access a `let` variable before it's declared in the code.
- **Reassignment but not redeclaration**: Variables declared with `let` can be reassigned, but you cannot redeclare them in the same scope.

#### const:

- **Block-scoped**: Like `let`, variables declared with `const` have block scope.
- **Hoisting**: Variables declared with `const` are also hoisted to the top of their block but are not initialized. You cannot access a `const` variable before it's declared.
- **Immutable reference**: Variables declared with `const` must be initialized with a value and cannot be reassigned to a different reference. However, their value may be mutable (e.g., properties of an object or elements of an array can be changed).

## Answers

### Answer by Ravi Vishwakarma

`let`, `const`, and `var` keywords are used for variable declarations, but they have some key differences in terms of scope, hoisting, and mutability

#### var:

- **Function-scoped or globally scoped**: Variables declared with `var` are function-scoped when declared inside a function, or globally scoped when declared outside any function.
- **Hoisting**: Variables declared with `var` are hoisted to the top of their scope and initialized with `undefined`. This means you can use a `var` variable before it's declared in the code.
- **Reassignment and redeclaration**: Variables declared with `var` can be redeclared and reassigned.

#### let:

- **Block-scoped**: Variables declared with `let` have block scope, which means they are only accessible within the nearest enclosing curly braces `{ }`.
- **Hoisting**: Variables declared with `let` are hoisted to the top of their block but are not initialized. This means you cannot access a `let` variable before it's declared in the code.
- **Reassignment but not redeclaration**: Variables declared with `let` can be reassigned, but you cannot redeclare them in the same scope.

#### const:

- **Block-scoped**: Like `let`, variables declared with `const` have block scope.
- **Hoisting**: Variables declared with `const` are also hoisted to the top of their block but are not initialized. You cannot access a `const` variable before it's declared.
- **Immutable reference**: Variables declared with `const` must be initialized with a value and cannot be reassigned to a different reference. However, their value may be mutable (e.g., properties of an object or elements of an array can be changed).


---

Original Source: https://www.mindstick.com/interview/33906/what-is-the-difference-between-let-const-and-var-when-declaring-variables-in-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
