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

PPTX
MULTI THREADING IN JAVA
PDF
Javascript foundations: scope
PPTX
Understanding react hooks
PPT
Jsp/Servlet
PPSX
Javascript variables and datatypes
PPT
Core java concepts
PPTX
Java exception handling
PPTX
Java 8 presentation
MULTI THREADING IN JAVA
Javascript foundations: scope
Understanding react hooks
Jsp/Servlet
Javascript variables and datatypes
Core java concepts
Java exception handling
Java 8 presentation

What's hot (20)

PPTX
Constructor in java
PPTX
Modern JS with ES6
PPTX
Control statements in java
PPT
C# Basics
PPTX
History Of JAVA
PPTX
ReactJS presentation.pptx
PPT
Java Threads and Concurrency
PDF
Understanding react hooks
PPTX
React hooks
PPT
Exception handling
PPTX
Introduction to es6
PDF
ES6 presentation
PDF
JavaScript - Chapter 11 - Events
PDF
Basics of React Hooks.pptx.pdf
PPTX
JavaScript Promises
PDF
React new features and intro to Hooks
PDF
Spring Security
PPTX
React-JS Component Life-cycle Methods
PDF
Angular 2 observables
Constructor in java
Modern JS with ES6
Control statements in java
C# Basics
History Of JAVA
ReactJS presentation.pptx
Java Threads and Concurrency
Understanding react hooks
React hooks
Exception handling
Introduction to es6
ES6 presentation
JavaScript - Chapter 11 - Events
Basics of React Hooks.pptx.pdf
JavaScript Promises
React new features and intro to Hooks
Spring Security
React-JS Component Life-cycle Methods
Angular 2 observables
Ad

Similar to Js scope (20)

PDF
Java scriptconfusingbits
PDF
Java scriptconfusingbits
PPTX
Javascript internals
PDF
Important JavaScript Concepts Every Developer Must Know
PDF
1669958779195.pdf
PPT
Web development basics (Part-4)
PPTX
Scope and closures
PPTX
Object oriented java script
KEY
The JavaScript Programming Primer
PPTX
Advanced javascript from zero to hero in this PPT
PPTX
JS knowing-nuances
PPTX
JavaScripts internals #1
PPTX
Javascripts hidden treasures BY - https://geekyants.com/
PDF
Let's JavaScript
PDF
JavaScript Cheatsheets with easy way .pdf
PDF
Comprehensive JavaScript Cheat Sheet for Quick Reference and Mastery
PDF
Intro to JavaScript - Week 2: Function
PPT
A Deeper look into Javascript Basics
PDF
Javascript under the hood 1
PDF
Variable hoisting in JavaScript
Java scriptconfusingbits
Java scriptconfusingbits
Javascript internals
Important JavaScript Concepts Every Developer Must Know
1669958779195.pdf
Web development basics (Part-4)
Scope and closures
Object oriented java script
The JavaScript Programming Primer
Advanced javascript from zero to hero in this PPT
JS knowing-nuances
JavaScripts internals #1
Javascripts hidden treasures BY - https://geekyants.com/
Let's JavaScript
JavaScript Cheatsheets with easy way .pdf
Comprehensive JavaScript Cheat Sheet for Quick Reference and Mastery
Intro to JavaScript - Week 2: Function
A Deeper look into Javascript Basics
Javascript under the hood 1
Variable hoisting in JavaScript
Ad

Recently uploaded (20)

PPTX
Transform Your Business with a Software ERP System
PPTX
Essential Infomation Tech presentation.pptx
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
ai tools demonstartion for schools and inter college
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
medical staffing services at VALiNTRY
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
history of c programming in notes for students .pptx
PPTX
L1 - Introduction to python Backend.pptx
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Introduction to Artificial Intelligence
PDF
System and Network Administration Chapter 2
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
Transform Your Business with a Software ERP System
Essential Infomation Tech presentation.pptx
Upgrade and Innovation Strategies for SAP ERP Customers
wealthsignaloriginal-com-DS-text-... (1).pdf
ai tools demonstartion for schools and inter college
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
2025 Textile ERP Trends: SAP, Odoo & Oracle
medical staffing services at VALiNTRY
Wondershare Filmora 15 Crack With Activation Key [2025
Design an Analysis of Algorithms I-SECS-1021-03
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
history of c programming in notes for students .pptx
L1 - Introduction to python Backend.pptx
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Introduction to Artificial Intelligence
System and Network Administration Chapter 2
VVF-Customer-Presentation2025-Ver1.9.pptx

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