Var VS Let VS Const in JavaScript

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.

Using var
Using let
Using co…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Muhammad Zubair

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

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.

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.

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

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

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

{
    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

{
    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

const Z="Good Morning"
console.log (Z)  // Good Morning
Z="Good Evening" // Error :Assignment to constant variable.
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

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 !! ⚡


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Muhammad Zubair


Print Share Comment Cite Upload Translate Updates
APA

Muhammad Zubair | Sciencx (2022-09-05T16:48:56+00:00) Var VS Let VS Const in JavaScript. Retrieved from https://www.scien.cx/2022/09/05/var-vs-let-vs-const-in-javascript-2/

MLA
" » Var VS Let VS Const in JavaScript." Muhammad Zubair | Sciencx - Monday September 5, 2022, https://www.scien.cx/2022/09/05/var-vs-let-vs-const-in-javascript-2/
HARVARD
Muhammad Zubair | Sciencx Monday September 5, 2022 » Var VS Let VS Const in JavaScript., viewed ,<https://www.scien.cx/2022/09/05/var-vs-let-vs-const-in-javascript-2/>
VANCOUVER
Muhammad Zubair | Sciencx - » Var VS Let VS Const in JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/05/var-vs-let-vs-const-in-javascript-2/
CHICAGO
" » Var VS Let VS Const in JavaScript." Muhammad Zubair | Sciencx - Accessed . https://www.scien.cx/2022/09/05/var-vs-let-vs-const-in-javascript-2/
IEEE
" » Var VS Let VS Const in JavaScript." Muhammad Zubair | Sciencx [Online]. Available: https://www.scien.cx/2022/09/05/var-vs-let-vs-const-in-javascript-2/. [Accessed: ]
rf:citation
» Var VS Let VS Const in JavaScript | Muhammad Zubair | Sciencx | https://www.scien.cx/2022/09/05/var-vs-let-vs-const-in-javascript-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.