JavaScript Scope

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 variable defined with let keyword at the very last, so it throws an error.

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.