Skip to main content

var Vs let Vs const



 
Here in this blog, we are going to study the difference between let, var, and const in Javascript

First Case,
var means variable
Here in the var condition, we can change the value of var means the variable identifier anywhere in the coding
For Ex:-
var a =2
var a =3
then console.log(a) would give you the output as 3 means the changed value of var.

Second Case
let means let us assume
Here in the let condition, we can update the value for the identifier let but we can't change it.
For ex:-
let a ="hello"
let a ="Anwar"
{let a ="Sabih"
console.log(a)}
console.log(a)
So the output for that will be 
Sabih
Hello
And the output for the let a ="Anwar" will show an error against it because let is the function for the block operable between this {} and outside that it can be assumed only once.
But if like a variable you want to assume then you can assume by the coding of
a = "Anwar" but it is stored as a variable.

Third case
const means constant
Here in constant we can't change the values of const in any case.
For ex:-
const author = "Anwar"
author = "Sabih"
let author ="shayaan"
var author = "Ghufrana"
So only the output will be as Anwar only and the codings for the red colored ones will show the error only because const can never be changed got it because once when you fix it. It can never be changed.


Comments