Var vs Let vs Const in JavaScript
var, let and const are just three keywords which are used to declare variables in JavaScript. Now, before deep diving into these 3 keywords, let's understand what a variable is. Variables are the kind of containers where values get stored. It acts like a box which occupies some space when it's declared, and then the value gets stored in it.

Scope in JS
Now, before knowing which keyword is used in which scenarios, let's first understand what scope is. Scope basically means a definite range within which a variable is accessible. Primarily, in JavaScript, there are two scopes - Global Scope and Local Scope. Global Scope comprises the whole document, inside which all variables, objects, functions, etc., are declared. Variables declared inside the global scope can be accessed throughout the program. Local Scope generally means the range which is created while defining the function. And variables declared inside a function have access only inside that particular function. If you try to use it outside the function, it will give a Reference Error.
Function Scope and Block Scope
Now, the local scope is also divided into two scopes - Function Scope and Block Scope. If a variable is declared inside a function scope, then it can be accessed throughout that particular function. Meanwhile, block scope has its demarcation. When variables are declared inside an if condition or inside a loop, and they have their access within that particular block only, then the variable is considered as block scoped. So if a variable has its accessibility inside the curly braces only, then that variable is termed as block scoped.
var, let and const
But here is a catch. Variables don't get automatically termed as function scoped or block scoped. It depends solely on its keyword. And that keyword would be one among var, let and const.

So, when a variable is declared using the keyword var, it's accessible within that particular function only. When it is used outside that particular function, it will show a reference error. That's why variables declared with the help of var are considered as function scoped.

As you can see in the above picture, I have declared the variable (num) using var. And it's completely accessible within the function (printNum). And it will generate the following output.

But if we try to access the value of variable num outside the function printNum.

It will give a reference error, as you can see in the picture.

But when the variable is declared using either const or let, then it's accessible only within a particular block, which can be either if conditions, loops or just a simple pair of curly braces.


As you can see in the above program, I have declared two variables, oddNum (using let) and evenNum (using const). Now, when I tried to access their value outside the if condition, it showed a reference error in both cases (let and const).


This shows that when a variable is declared with either const or let, it will be only accessible within that particular block. That's why variables declared with let and const are considered to be block scoped.
Difference between let and const
When we declare a variable using let, we can change its value further in the program.

As you can see, I have declared a variable (a) using let and initialised it with 5. But after a few lines, I have changed its value to 7. Now, when I run this program, it will give the following output.

As you can see in the output, first it printed the value 5, while the second time it printed the value 7.
But, in the case of const, once we initialise the variable with a particular value, we can't change it further throughout the program. const makes that variable constant, which means it can't be changed in the future. Let's understand it with an example.

As you can see, I have declared the variable (a) using const and initialised it with 5. But again, after a few lines, I have changed its value to 7. Now, when I run this program, it will give the following output.

As you can see, the value of a is printed at first without any hindrance, which is 5. But as I change its value to 7, it gives an error with the following statement :
TypeError: Assignment to constant variable.
So, once you declare a variable using the keyword const, you can't change its value anywhere in the program.
Conclusion
So basically, what we concluded is that var, let and const are 3 keywords which we use to declare a variable. When we declare a variable using var, it's considered as function scope, which means it's accessible within that particular function only. And, when we declare a variable with either let or const, it's considered as block scope, which means it's accessible within a particular block only, which can be either if conditions, loops, or a pair of curly braces. And the only difference between let and const is that we can change the value of a variable if we declare it with let. Otherwise, if we declare it using const, then changing the value of a variable is impossible. It makes it a constant.