Difference between var, let and const in JavaScript

Frontend Engineer
The var, let and const are the keyword used to declare a variable in Javascript, var is the old way to declare the variable in Javascript, and the let and const are new and comes into existence in ES6. when we seat for a frontend dev interview, the interviewer ask this question many times what's the difference between these keywords, we will look here the difference between them in points
- Scope
- Declaration
- Declaration without Initialisation
- Re-Initialisation
Scope
Scope in Javascript is a certain region of a program where the defined variable can exist and recognize, beyond that we can't recognize that variable,
- the var keyword is function scope and global scope that globally accessible throughout the page
//for example:- here I'm declaring variable 'a' inside a block scope where 'a' stores the value 9
{
var a=9;
}
//now i'm trying to access this outside the scope
console.log(a);
The Result
9
this shows that we can access the var declared variable globally throughout the page
- the let and const are the block scope keyword that can be accessible only inside their block scope beyond that it can't
{
let a=9;
const b=10
}
console.log(a);
console.log(b);
This will show an uncaught reference error: a is not defined
when we try to access this value inside their block, it will not show any error
{
let a=9;
const b=10
console.log("the value of a is "+ a);
console.log("the value of b is " +b);
}
Result
the value of a is 9
the value of b is 10
hence this shows that let and const are the block scope
Declaration
we can redeclare the var declared variable but we can't redeclare the let & const declare variable
for example
var a;
var a;
//it is valid we can do that, we can redeclare the variable
when we declared variable using let and const, now if we try to redeclare the the same variable it will shows an error
let a;
let a;
console.log("hello")
Result
uncaught syntaxError: redeclaration of let a
now we are doing using const
const a;
const a;
console.log("hello")
here the first line of code will shows an error because we can't declare the variable using the const without initialising any value to it,
Declaration without initialization
we can declare the variable using var and let without initialising any value to it, but we can't declare the variable using const without initialising any value to it,
var a;
//this is valid
let b;
//this is also valid
const c;
//this is not valid
Re-initialisation of variable value
if we want to update the value of variable, we can do that only when we have defined the variable using var and let, if we declared the variable using the const keyword we can't reinitialize variable value,
for example
var a=6;
a=7;
let b=10;
b=12;
console.log(a);
console.log(b);
Result
7
12
If we try this same using the const keyword
const c=12;
c=56;
console.log(c);
Result
Uncaught TypeError: invalid assignment to const 'c'
This are the some main differences between var, let and const
Thankyou for reading

