Let, Var and Const in Javascript

Var

Var keyword has global Or Function scope, it can be re-declared inside its scope, variable should be followed by var.

function varGlobalScope(){
var y=90; // declare
if(true){
console.log(y); // 90
}
var y=89;//re-declare
console.log(y) //89
}

Let

Let keyword has Block scope, It can’t be re-declared and can’t access out of block, variable should be followed by let.

function letRedeclare(){
if(true){
let y=25;
console.log(y); // 25
let y=90; /* Uncaught SyntaxError: Identifier 'y' has already been declared. **/
}
}
function letOutsideOfBlock(){
if(true){
let x=90;
console.log(x)//90
}
console.log(x);// Uncaught ReferenceError: x is not defined
}

Const

Const behave same as let but you can’t change the value once you initialise, variable should be followed by Const.

function letOutsideOfBlock(){
if(true){
const x = 90;
console.log(x) // 90
try{
x=78;
}catch(error){
console.log(error)// TypeError: Assignment to constant.
}
console.log(x) // 90
}
console.log(x);// Uncaught ReferenceError: x is not defined
}