In JavaScript we can define variable by using “var” and “let” keyword. JavaScript scope works slightly different way than other object oriented programming language like java.
In the below example please look how the variables are defined:-
Here “var” keyword works differently than the “let” keyword. Here “let” will not be executed at the very first but it depends on the position. Move the variable declaration before using it, then it will works fine.
That is why the “var” is called the functional scope and “let” is called the block scope.