What is lexical scope in javascript?
A scope in programming refers to the area in which a variable or function is visible.There are two types of scope we know,
- Global Scope
- Local Scope
The term "Lexical scope" refers to the ability of a function to access the variables inside and outside of that function. Simply saying, that a variable defined outside a function can be accessed by another function defined after the variable declaration. On the other hand, variables defined inside a function are not accessible outside of it.
var x = 5; // available to all functions var parent = function (){ // parent function var y = 10; var child = function (){ // child function var z = 30; } return; }
- In the above code variable "z" accessed only inside "child" function whereas variable "y" can be accessed by "child" and "parent" function.
- Variable "x" available for all the functions inside and outside of parent function.
Comments (0)