We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 6
Lexical scoping
in JavaScript
foe ‘
Gioba : co-edKS
an
Ps
ws
What is lexical scoping?
Lexical scoping is a convention used in many
programming languages that sets the scope
of a variable so that it may only be called
(referenced) from within the block of code in
which it is defined. During runtime, JavaScript
does these two things: parsing and execution.
Lexical scoping takes place during the parsing
phase of the js engine.
hy
Swipe —3/6
hy
The ability of a function scope to
access variables from the parent
scope is called Lexical scope. The
child function is lexically bonded to
the parent But the opposite is not
true; the variables defined inside a
functioncould not be accessed
outside that function.TE
Access from
inner scope
Can access variables
from inner scope
of outer scope.
Global
Scope
Outermost
function
etal
function
Tala aattel-i9
function
Access from
outer scope
level hele BVelilelel(
from outer scope
of inner scope.
In the above diagram, we can see that, due to
lexical scope, the functions may access alll
variables from their parent scopes up to the
global scope, but no scope may access any
variables from the functions defined inside it.
hy
Swipe —KIL
In this example function displayModel() has
access to model because of lexical scoping
Function displayModel() contains reference of
function car().
Variable mode] is part of the local scope and
attached to function diaplayModel().
eo function car{){ ~
var model = "Roadster";
function displayModel( ){
console. log(model);
3
return displayModel;
5
a4
“Roadster”
var chooseCar = car();
chooseCar( ); LZ.
: ee
hy)
Swipe —Slobodan Gajié
Content Creator
oe
baal} ye)
FOLLOW FOR MORE