Know the concept and save yourself
As we know there is two way to declare a variable either declare with var or declare let. Now the matter is when to use var and when to use let. For this, we need to know the main difference between both.
The main difference between let and var is that scope of a variable defined with let is limited to the block in which it is declared while a variable declared with var has the global scope. So we can say that var is rather a keyword that defines a variable globally regardless of block scope.
Here is an example program, see I got the output from the outside scope. We can access var from anywhere inside the function that’s why it is also known as function scope.
Have a great day!