Javascript Scopes
By Saket Bhatnagar••Beginner to Intermediate
scope
- 1Scope defines the visibility or accessibility of a variable.
We have Two scopes
- 1Global Scope
- 2Local Scope
Global scope
- 1The variable declared in global scope can be accessed anywhere in the program.
- 2Global scope has the highest accessibility.
- 3Variable declared with var goes in Global scope.
Local scope
- 1Local/block scope/function scope
- 2The variable declared in local scope can be accessed in that block only i.e. we can not access the variable from outside.
- 3JS engine creates local scope for functions and blocks.
- 4
Function's Local Scope
- Local scope created for function is refered as function scope.
- Variable's declared in function's scope can not be accessed from outside. - 5
Block's Local Scope
- Local scope created for block is refered as block scope.
- Variable's declared in block scope can not be accessed from outside.
- But only variables declared with var are accessible from outside of block.
Note: Variables declared with let and const are also locally scoped.
Firefox represent it as - Block scope.
Chrome represent it as - Script scope.