Var VS Let VS Const in JavaScript
Tags: javascript, webdev, beginners, programming
Published: 2022-09-05 16:48:56

Hey 👋, Guys Welcome.

Today we are deeply learning , that what's the difference between LET , VAR & CONST Keyword in JavaScript.

Let's Dive In ..

---

In JavaScript there are mainly 3 methods to declare the variables.

1. Using var
2. Using let
3. Using const

---

## 1) Using var
Before the release of (1995-2015) ECMA Script 6 (ES6) in 2015 ,  This was the keyword most used to declare variables , arrays or objects etc.

**Example**

```javascript
var X=12;
console.log(X) // 12

var Y="John Doe";
console.log(Y) // John Doe

var Z=[1,2,3,4,5];
console.log(Z) // [1,2,3,4,5]
```

##2) Using let

After the release of ECMA Script 6 (ES6) in 2015 ,  This was the keyword most used to declare variables , arrays or objects etc.

```javascript
let X=12;
console.log(X) //12

let Y="John Doe";
console.log(Y) //John Doe

let Z=[1,2,3,4,5];
console.log(Z) //[1,2,3,4,5]
```

##2) Using const

After the release of ECMA Script 6 (ES6) in 2015 ,  This was the keyword most used to declare variables , arrays or objects etc.

```javascript
const X=12;
console.log(X) //12

const Y="John Doe";
console.log(Y) //John Doe

const Z=[1,2,3,4,5];
console.log(Z) //[1,2,3,4,5]
```

As you can see that we use all these three methods to declare and initialize the variables , now we will discuss few things that differentiate them

### Re-Declaration of Variable

With the help of var keyword, we can re-declare the variable with same identifier , see below example

```javascript
var X=21;
var X=12;

console.log(X) // 12
```

Now , If we try to re-declare the variable with same identifier with let or const keyword , this will generate the error

```javascript
let X=21;
let X=12;

console.log(X) // Identifier 'X' has already been declared
```

## Scope (Global and Block)

In JavaScript everything written inside the curly braces {} is considered as block. var has function scope if a variable is declared using var keyword it will be accessible throughout the function.

In Given below example you can see that if we declare the variable with let keyword and try to access it outside the block , this can not be accessed and give error

``` javascript
{
    let X=12
}
console.log(X) // X is not defined
```
But if we do the same thing with var keyword , then there will be no error and variable can be easily accessed outside block scope , see example

```javascript
{
    var X=12
}
console.log(X) // 12
```

## Immutability

Now Let's discuss something about const keyword. with const , once its initialized , its value (reference) can never be changed , that's why they are immutable

```javascript
const Z="Good Morning"
console.log (Z)  // Good Morning
Z="Good Evening" // Error :Assignment to constant variable.
```

```javascript
const Arr=[1,2,3,4,5]
Arr[1]=1.5
console.log(Arr) // [1,1.5,3,4,5]
```

const allows us to only modify the value of the variable (array or objects etc), but the reference to the array cannot be changed, for example

```javascript
const Arr=[1,2,3,4,5]
console.log(Arr) // [1,2,3,4,5]
Arr=[1,5,6,7,8]  // Error: Assignment to constant variable.
```

These Were the Major Differences in these three keywords. Now its totally depends upon you , that which one you preferr to use. The let & const are widely used nowadays 


Hope you liked my article 💛. I will meet you in the next post with something new to learn.

Happy Coding !! ⚡