Tuesday, 1 Jul 2025
  • My Feed
  • My Interests
  • My Saves
  • History
  • Blog
Subscribe
Code Reveals
  • Home
  • HTML
    What is Symentic HTML

    What is Symentic HTML?

    By Chief Editor

    What is Doctype HTML in HTML?

    By Chief Editor

    What is a Meta Tag in HTML?

    By Chief Editor

    What are the different types of HTML tags?

    By Chief Editor

    What is Block level Element and Inline Level Element?

    By Chief Editor

    What are the async and defer attributes in the “script” tag?

    By Chief Editor
  • JavaScript

    What is the Event Loop in JavaScript?

    By Chief Editor

    What are the Rest and Spread operators in JavaScript?

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor
    What are the Lexical Scope in JavaScript

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    What are the map, filter, and reduce methods in JavaScript?

    By Chief Editor

    What is memoization in JavaScript?

    By Chief Editor
  • Frontend Interview

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    What is Position in CSS?

    By Chief Editor

    What is Symentic HTML?

    By Chief Editor

    What is Block level Element and Inline Level Element?

    By Chief Editor
  • Backend Interview

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor

    What is Symentic HTML?

    By Chief Editor

    Mastering Star Rating Systems: Best Practices and Optimized Code Examples

    By Chief Editor

    What are controlled and uncontrolled components in React?

    By Chief Editor

    What is one-way data binding in React?

    By Chief Editor

    What are the async and defer attributes in the “script” tag?

    By Chief Editor
  • Other
    • Contact Us
  • Frontend Interview
  • Backend Interview
  • React Interview
  • JavaScript Interview
  • Contacts Us
  • Advertise with Us
  • Complaint
  • Privacy Policy
  • Cookie Policy
  • Submit a Tip
  • 🔥
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • React Interview
  • HTML
  • Frontend Interview
  • CSS
  • Redux
  • Javascript
  • System Design
Font ResizerAa
Code RevealsCode Reveals
  • My Saves
  • My Interests
  • My Feed
  • History
  • Technology
Search
  • Homepage
  • Pages
    • Home
    • Blog Index
    • Contact Us
    • Search Page
    • 404 Page
  • Features
    • Post Headers
    • Layout
  • Personalized
    • My Feed
    • My Saves
    • My Interests
    • History
  • About
  • Categories
    • Technology
  • Categories
Have an existing account? Sign In
Follow US
© 2022 Code Reveals Inc. All Rights Reserved.

Home What are the Lexical Scope in JavaScript?

Frontend InterviewJavaScript

What are the Lexical Scope in JavaScript?

Chief Editor
Last updated: February 14, 2025 7:50 am
Chief Editor
Share
What are the Lexical Scope in JavaScript
SHARE

Lexical scope in JavaScript means that the scope of a variable is determined by its position in the source code (where it is written) and the nested structure of functions.

Contents
How Lexical Scope Works:Example of Lexical Scope:Key Points to Remember:Practical Example Using Scope Chain:Summary:

In other words:

  • Inner functions have access to variables declared in their outer functions.
  • Functions are executed using the scope in which they were defined, not where they are called.

How Lexical Scope Works:

When JavaScript looks for a variable, it starts in the local scope (inside the current function), and if it doesn’t find it there, it moves up to the outer scope (where the function was defined), and continues going up until it reaches the global scope.

This process is called Scope Chain.


Example of Lexical Scope:

javascriptCopyEditfunction outerFunction() {
  let outerVar = 'I am from outer';

  function innerFunction() {
    let innerVar = 'I am from inner';
    console.log(outerVar); // Accessing outer variable
    console.log(innerVar); // Accessing inner variable
  }

  innerFunction();
}

outerFunction();

Output:

cssCopyEditI am from outer
I am from inner
  • innerFunction() has access to outerVar because of lexical scope.
  • outerFunction() doesn’t have access to innerVar because it is inside the inner scope.

Key Points to Remember:

ConceptDescription
Lexical ScopeScope is defined by where variables and functions are declared (written), not where they are called.
Scope ChainIf a variable is not found in the current scope, JS looks in the outer scope and continues until the global scope.
Inner FunctionsHave access to variables in their outer functions.
Global ScopeAccessible from anywhere in the code.
Local Scope (Block/Function Scope)Accessible only within the block or function where it’s declared.

Practical Example Using Scope Chain:

javascriptCopyEditlet globalVar = 'I am global';

function outer() {
  let outerVar = 'I am outer';

  function inner() {
    console.log(globalVar); // Accesses global scope
    console.log(outerVar);  // Accesses outer scope
  }

  inner();
}

outer();

Output:

cssCopyEditI am global
I am outer

This demonstrates lexical scope and how JavaScript looks up variables using the scope chain.


Summary:

✅ Lexical scope is based on the structure of your code (where functions and variables are written).
✅ Inner functions can access variables from outer functions (scope chain).
✅ Scope is determined at the time of writing, not at runtime.

This concept is fundamental to closures, callbacks, and modern JavaScript development.

Share This Article
Email Copy Link Print
Previous Article What are the async and defer attributes in the “script” tag?
Next Article What are Benefits in React?
Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Your Trusted Source for Accurate and Timely Updates!

Our commitment to accuracy, impartiality, and delivering breaking news as it happens has earned us the trust of a vast audience. Stay ahead with real-time updates on the latest events, trends.
FacebookLike
XFollow
InstagramFollow
YoutubeSubscribe
LinkedInFollow
QuoraFollow
- Advertisement -
Ad imageAd image

Popular Posts

What is hoisting in JavaScript with an example?

Hoisting is a behavior in JavaScript where variables and function declarations are moved to the…

By Chief Editor

How to Reverse a String in JavaScript: Two Essential Methods

Reversing a string is one of the most common beginner-level tasks in programming. It’s a…

By Chief Editor

Lazy Loading in React.js: Boosting Performance and Reducing Load Time

In modern web development, performance plays a crucial role in user experience. One technique that…

By Chief Editor

You Might Also Like

JavaScriptJavaScript Interview

What is one-way data binding in React?

By Chief Editor
What is Symentic HTML
Frontend InterviewHTML

What is Symentic HTML?

By Chief Editor
JavaScript

Explain var let and const in JavaScript with Example.

By Chief Editor
JavaScriptJavaScript Interview

What is Callback Hell in JavaScript?

By Chief Editor

Code Reveals is a cutting-edge software development company dedicated to delivering high-quality, scalable, and innovative solutions for businesses of all sizes. Our team of expert developers, designers, and engineers specializes in creating custom software, web applications, mobile apps, and enterprise solutions that are tailored to meet the unique needs of our clients.

Most Famous
  • HTML
  • CSS
  • JavaScript
  • Node
Top Categories
  • Frontend Interview
  • Backend Interview
  • React Interview
  • JavaScript Interview
Usefull Links
  • Contacts Us
  • Advertise with Us
  • Complaint
  • Privacy Policy
  • Cookie Policy
  • Submit a Tip

©2025  Code Reveals Inc. All Rights Reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?