let and const are introduced in ES6 so they are modern Javascript. var is an old way of declaring variables. So in this blog you’ll how and where to use var let and const in javascript.
Variable Declaration with var
The scope of the var variable is global outside the function. But before moving further you should know what is the scope of the variable.
Scope: Scope means where we can use these variables or where these variables are available for use.
var a = 20; //Global scope
function varNum() {
var a = 15; //local scope
console.log(a);
}
varNum();
console.log(a);
Output:

Variable Declaration with let
let allows you to declare variables that are limited to the scope of a block statement.
if (true) {
let a = 15;
console.log(a);
}
console.log(a); //error
Output:

Variable Declaration with const
Declaring a variable with const is similar to let, it comes in Block Scope. and the value of the const variable will be never changed.
const a = 15;
a = 12;
console.log(a);
Output:

Previous Post: What are Variables in JavaScript?