SlideShare a Scribd company logo
3
Most read
6
Most read
8
Most read
JS Scope, Lexical Scope,
Hosting Concept
What is Scope?
Scope means lifespan of a variables. It determine accessibility/visibility of the variables
inside the JavaScript code. There are three types of scope.
● Global
● Local
● Blocked(ES6)
Global Scope
The variable which is declared in the global
execution context is called Global Scope. Global
Scope variable can accessible from anywhere
within the code.
Local Scope
Locally scoped variables are only visible and accessible within their local scope. There are 2
local scopes.
1. Function
2. Blocked
Function Scope
The variable which is declared inside the
function is called function scope. The function
scope variable cannot be accessed or modified
outside the function. For example:
In this example, variable “a” is a local scope of
function func(). It can be accessible only inside
of function func() not in outside of the func()
function. It will throw an error if we try to access
function scope outside of function.
Block Scope
The variable which is declared inside the curly
braces (If, for, while loops) called block scope. The
block scope variable can be accessed only within
the block of code not outside of the curly braces.
“Let” and “const” keywords used to declare the
block scope variable.
In the example, b and c are blocked scope variable
which we cannot access or modify outside the curly
braces. When we try to access the variable b and c,
it throw an reference error.
Lexical Scope
Scope means, the set of rules that govern how the engine can look up the variable by its identifier . The
lexical scope is defined in compiler phase. When JS engine execute the variable in the code, it look up the
variable in current scope. If it’s not available in current scope, then JS engine goes level one up and then
look up the variable in outer scope. If it’s not available in the outer scope, then it will goes one level up again
until it reach global scope. If it’s available in global scope, then it will return the value otherwise it will return
reference error.
Lexical Scope
Lexical Scope
In the above example,When JS engine execute console.log(a), it starts lookup variable “a” in the current
scope (innerFunc scope). Since the variable “a” is not available in the scope, JS engine goes one level up
and lookup the variable “a” in outer scope (outerFunc scope). Since the variable “a” is also not available in
the outer scope as well, so the JS engine goes level up again and look up the variable in global scope. The
JS finally finds the value a in global scope and then JS engine return value 5 and display it in log. If variable
“a” is not defined in anywhere in code, then it will return “ “ReferenceError: a is not defined” error.
Hoisting
Before we get into hoisting topic. What you think about below code block
Hoisting
Before JS engine execute the code by line by line, it
scan entire code once and if it’s finds a variable start
with “var” keyword, then it store that variable as
“undefined” in memory under the current scope. If JS
engine finds function declaration starts, then it
stores entire function declaration in memory under
the current scope.
Let consider this code,
Reference:
1. https://medium.com/@a.mahesh06/js-scope-lexical-scope-hoisting-concept-ca6b08c6eaf
2. https://medium.com/@a.mahesh06/how-javascript-execution-works-is-js-a-compiler-or-
interpreter-540e4f829582
Code Snippet :
1. https://carbon.now.sh/?bg=rgba(171%2C%20184%2C%20195%2C%201)&t=a11y-
dark&wt=none&l=javascript&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=56
px&ph=56px&ln=false&fl=1&fm=Hack&fs=14px&lh=133%25&si=false&es=2x&wm=false&c
ode=1%2520console.log(a)%253B%2520%250A2%2520var%2520a%2520%253D%25205
%253B%250A3%2520function%2520func()%257B%250A4%2520%2520%2520console.lo
g(b)%253B%250A5%2520%2520%2520var%2520b%2520%253D%252010%253B%250A
6%2520%257D%250A7%2520func()%253B
2. https://snipit.io/lists/0/34819
Thank you
- Santhosh Kumar S

More Related Content

What's hot (20)

PPTX
Core java complete ppt(note)
arvind pandey
 
PPTX
Exceptions in Java
Vadym Lotar
 
PDF
Spring Boot
Jaydeep Kale
 
PDF
React js use contexts and useContext hook
Piyush Jamwal
 
PPTX
Inner classes in java
PhD Research Scholar
 
PPTX
React-JS.pptx
AnmolPandita7
 
PDF
Angular Advanced Routing
Laurent Duveau
 
PDF
JavaScript Programming
Sehwan Noh
 
PPTX
jQuery
Dileep Mishra
 
PDF
Important React Hooks
Knoldus Inc.
 
PPTX
Core Java Tutorials by Mahika Tutorials
Mahika Tutorials
 
PDF
React Router: React Meetup XXL
Rob Gietema
 
PPTX
React hooks
Ramy ElBasyouni
 
PPT
Javascript arrays
Hassan Dar
 
PPTX
JAVA AWT
shanmuga rajan
 
PPTX
Bootstrap PPT Part - 2
EPAM Systems
 
PDF
javascript objects
Vijay Kalyan
 
PDF
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
PDF
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Edureka!
 
PPTX
Nodejs functions & modules
monikadeshmane
 
Core java complete ppt(note)
arvind pandey
 
Exceptions in Java
Vadym Lotar
 
Spring Boot
Jaydeep Kale
 
React js use contexts and useContext hook
Piyush Jamwal
 
Inner classes in java
PhD Research Scholar
 
React-JS.pptx
AnmolPandita7
 
Angular Advanced Routing
Laurent Duveau
 
JavaScript Programming
Sehwan Noh
 
Important React Hooks
Knoldus Inc.
 
Core Java Tutorials by Mahika Tutorials
Mahika Tutorials
 
React Router: React Meetup XXL
Rob Gietema
 
React hooks
Ramy ElBasyouni
 
Javascript arrays
Hassan Dar
 
JAVA AWT
shanmuga rajan
 
Bootstrap PPT Part - 2
EPAM Systems
 
javascript objects
Vijay Kalyan
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Edureka!
 
Nodejs functions & modules
monikadeshmane
 

Similar to Js scope (20)

PDF
Java scriptconfusingbits
ColdFusionConference
 
PDF
Java scriptconfusingbits
ColdFusionConference
 
PPTX
Javascript internals
Nir Noy
 
PDF
Important JavaScript Concepts Every Developer Must Know
yashikanigam1
 
PDF
1669958779195.pdf
venud11
 
PPT
Web development basics (Part-4)
Rajat Pratap Singh
 
PPTX
Scope and closures
Monu Chaudhary
 
PPTX
Object oriented java script
vivek p s
 
KEY
The JavaScript Programming Primer
Mike Wilcox
 
PPTX
JS knowing-nuances
Ramesh Ramalingam
 
PPTX
JavaScripts internals #1
Martin Pernica
 
PPTX
Javascripts hidden treasures BY - https://geekyants.com/
Geekyants
 
PDF
Let's JavaScript
Paweł Dorofiejczyk
 
PDF
Comprehensive JavaScript Cheat Sheet for Quick Reference and Mastery
pavanbackup22
 
PDF
JavaScript Cheatsheets with easy way .pdf
ranjanadeore1
 
PDF
Intro to JavaScript - Week 2: Function
Jeongbae Oh
 
PPT
A Deeper look into Javascript Basics
Mindfire Solutions
 
PDF
Javascript under the hood 1
Thang Tran Duc
 
PDF
Variable hoisting in JavaScript
Ideas2IT Technologies
 
PDF
Alessandro (cirpo) Cinelli - Dear JavaScript - Codemotion Berlin 2018
Codemotion
 
Java scriptconfusingbits
ColdFusionConference
 
Java scriptconfusingbits
ColdFusionConference
 
Javascript internals
Nir Noy
 
Important JavaScript Concepts Every Developer Must Know
yashikanigam1
 
1669958779195.pdf
venud11
 
Web development basics (Part-4)
Rajat Pratap Singh
 
Scope and closures
Monu Chaudhary
 
Object oriented java script
vivek p s
 
The JavaScript Programming Primer
Mike Wilcox
 
JS knowing-nuances
Ramesh Ramalingam
 
JavaScripts internals #1
Martin Pernica
 
Javascripts hidden treasures BY - https://geekyants.com/
Geekyants
 
Let's JavaScript
Paweł Dorofiejczyk
 
Comprehensive JavaScript Cheat Sheet for Quick Reference and Mastery
pavanbackup22
 
JavaScript Cheatsheets with easy way .pdf
ranjanadeore1
 
Intro to JavaScript - Week 2: Function
Jeongbae Oh
 
A Deeper look into Javascript Basics
Mindfire Solutions
 
Javascript under the hood 1
Thang Tran Duc
 
Variable hoisting in JavaScript
Ideas2IT Technologies
 
Alessandro (cirpo) Cinelli - Dear JavaScript - Codemotion Berlin 2018
Codemotion
 
Ad

Recently uploaded (20)

PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PPTX
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
Ad

Js scope

  • 1. JS Scope, Lexical Scope, Hosting Concept
  • 2. What is Scope? Scope means lifespan of a variables. It determine accessibility/visibility of the variables inside the JavaScript code. There are three types of scope. ● Global ● Local ● Blocked(ES6)
  • 3. Global Scope The variable which is declared in the global execution context is called Global Scope. Global Scope variable can accessible from anywhere within the code.
  • 4. Local Scope Locally scoped variables are only visible and accessible within their local scope. There are 2 local scopes. 1. Function 2. Blocked
  • 5. Function Scope The variable which is declared inside the function is called function scope. The function scope variable cannot be accessed or modified outside the function. For example: In this example, variable “a” is a local scope of function func(). It can be accessible only inside of function func() not in outside of the func() function. It will throw an error if we try to access function scope outside of function.
  • 6. Block Scope The variable which is declared inside the curly braces (If, for, while loops) called block scope. The block scope variable can be accessed only within the block of code not outside of the curly braces. “Let” and “const” keywords used to declare the block scope variable. In the example, b and c are blocked scope variable which we cannot access or modify outside the curly braces. When we try to access the variable b and c, it throw an reference error.
  • 7. Lexical Scope Scope means, the set of rules that govern how the engine can look up the variable by its identifier . The lexical scope is defined in compiler phase. When JS engine execute the variable in the code, it look up the variable in current scope. If it’s not available in current scope, then JS engine goes level one up and then look up the variable in outer scope. If it’s not available in the outer scope, then it will goes one level up again until it reach global scope. If it’s available in global scope, then it will return the value otherwise it will return reference error.
  • 9. Lexical Scope In the above example,When JS engine execute console.log(a), it starts lookup variable “a” in the current scope (innerFunc scope). Since the variable “a” is not available in the scope, JS engine goes one level up and lookup the variable “a” in outer scope (outerFunc scope). Since the variable “a” is also not available in the outer scope as well, so the JS engine goes level up again and look up the variable in global scope. The JS finally finds the value a in global scope and then JS engine return value 5 and display it in log. If variable “a” is not defined in anywhere in code, then it will return “ “ReferenceError: a is not defined” error.
  • 10. Hoisting Before we get into hoisting topic. What you think about below code block
  • 11. Hoisting Before JS engine execute the code by line by line, it scan entire code once and if it’s finds a variable start with “var” keyword, then it store that variable as “undefined” in memory under the current scope. If JS engine finds function declaration starts, then it stores entire function declaration in memory under the current scope. Let consider this code,
  • 12. Reference: 1. https://medium.com/@a.mahesh06/js-scope-lexical-scope-hoisting-concept-ca6b08c6eaf 2. https://medium.com/@a.mahesh06/how-javascript-execution-works-is-js-a-compiler-or- interpreter-540e4f829582 Code Snippet : 1. https://carbon.now.sh/?bg=rgba(171%2C%20184%2C%20195%2C%201)&t=a11y- dark&wt=none&l=javascript&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=56 px&ph=56px&ln=false&fl=1&fm=Hack&fs=14px&lh=133%25&si=false&es=2x&wm=false&c ode=1%2520console.log(a)%253B%2520%250A2%2520var%2520a%2520%253D%25205 %253B%250A3%2520function%2520func()%257B%250A4%2520%2520%2520console.lo g(b)%253B%250A5%2520%2520%2520var%2520b%2520%253D%252010%253B%250A 6%2520%257D%250A7%2520func()%253B 2. https://snipit.io/lists/0/34819