- Local Variable
- Global Variable
ES6 has introduce two new keywords for declaring variables,
The scope of variables declared using var is within the function it is defined and global if it is not defined inside any function, while the scope of let is restricted to within the enclosing block it was declared in and global if it is not defined inside any enclosing block.
Const keyword :
The working of const is the same as that of let, except that variables declared using const cannot be changed (reassigned). Hence, const is used for constants. However, an entire constant cannot be reassigned but their properties can be changed. For example:
Local variable: When a variable is declared inside the function it has local scope. The local variable only visible and accessed within the function where it was declared. The local variable dies when function finished its task.
Global Variable: When the variable is declared outside of the function it becomes global variable and can be accessed from anywhere in the code.
For above code output value of inFor is 5 and we can’t access the i value outside so it will generate output message as Uncaught ReferenceError: i is not defined.
Static Variable scoping:
In above code, all three variable instance, headline, description are private variable belongs to the simplFunObject function object. The function simpleFunObject is a functional object and we can assign it variable and called render() method. Render refers to show output in the browser and utilize the document object to manipulate DOM elements.