Android App

TechLogic
Lexical Scoping defines how variable names are resolved in nested functions.

In other words, lexical scope means that in a nested group of functions, the inner functions have access to the variables and other resources of their parent scope.

Lexical scoping also known as static scoping refers to determining a variable's scope based solely on its position within the textual corpus of code.

This concept is heavily used in closures in JavaScript.
 

Dynamic scope defines global variables that can be called or referenced from anywhere after being defined. Sometimes they are called global variables, even though global variables in most programming languages are of lexical scope.

Following are some examples of Lexical scoping in JS

  •  A lexical scope in JavaScript means that a variable defined outside a function can be accessible inside another function defined after the variable declaration.
    But the opposite is not true i.e. the variables defined inside a function will not be accessible outside that function.

  Example:



  •  In simpler way, lexical scope is a variable defined outside your scope or upper scope is automatically available inside your scope which means you don't need to pass it there.
   Example:

  •  As per the JavaScript definition guide, in lexical scoping "functions are executed using the scope chain that was in effect when they were defined"
     Example:

1 Comments

Post a Comment