0% found this document useful (0 votes)
11 views18 pages

004-Scope Chain

Uploaded by

mamirmohammad514
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views18 pages

004-Scope Chain

Uploaded by

mamirmohammad514
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

004-JavaScript

Scope Chain
EHSAN EHRARI
Expressions in Scope Chain

• Scooping
• Lexical Scooping
• Scope
• Scope of a Variable
Scoping

• How our program vairalbels access and organized.


• Where do variables Live?
• Where can we access a certain variable and where not?
Lexical Scoping

• In JavaScript, lexical scoping is a feature of


the language that determines the scope of
variables based on their position in the
code, also known as their lexical
environment.
• Lexical scoping means that the scope of a
variable is determined at the time the
variable is declared, based on its location
in the code.
Scope

• Scope or Environment in which a certain variable is


declared (Variable Environment in case of Function).
• There are actually two main types of scope in JavaScript:
global scope and local scope. However, some people
may refer to three types of scope by distinguishing
between block scope and function scope.
Scope Types in JS

1.Global scope: Variables declared outside of any functions or blocks have global
scope, which means they can be accessed from any part of the program,
including inside functions and blocks.

2.Local scope: Variables declared inside functions or blocks have local scope,
which means they can only be accessed within the function or block in which they
are declared.

3.Block scope: Starting from ES6 (ECMAScript 2015), JavaScript introduced the let
and const keywords for declaring variables. Variables declared with these keywords
have block scope, which means they are only accessible within the block in which
they are declared, including any nested blocks.
Example
Scope of a Variable

• Region of our code where a certain variable can be accessed.


• So far we learned:
1. Scoping
2. Lexical Scoping
3. Scope
4. Scope of a Variable
• Some people use the keyword Scope for all of these.
• But they are a different.
What is Scope Chain?

• When a variable is accessed within a function or block of


code, the JavaScript engine first looks for the variable in
the current scope.
• If the variable is not found in the current scope, it looks for
the variable in the next outer scope, and so on until it
reaches the global scope. This process is called the scope
chain.
Example
Example
Example
Example
Example
Example
Scope Chain Call Stack
Summery

• Three types of Scope: Global, Function, Block


• Only let and const are block scope.
• Lexical Scoping
• Every scope always has access to all variables from all its
outer scopes. This is Scope chain.
• When a variable is not in the current scope, the engine
looks in the scope chain until it finds the variable its
looking for. This is called variable lookup.
• Scope chain is one way street.
Question?

You might also like