Wednesday, 15 Oct 2025
  • My Interests
  • My Saves
  • Try Intents
Subscribe
Focus - Code Reveals
  • Home
  • HTML

    What is a Meta Tag in HTML?

    By Chief Editor

    Difference between HTML Tag and HTML Element in HTML?

    By Chief Editor

    What is Doctype HTML in HTML?

    By Chief Editor

    What is the difference between “HTML” and “HTML5”?

    By Chief Editor

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

    By Chief Editor
    What is Symentic HTML

    What is Symentic HTML?

    By Chief Editor
  • JavaScript

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

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    What is the this Keyword in JavaScript?

    By Chief Editor

    Explain Call, Apply and Bind in JavaScript.

    By Chief Editor

    What is hoisting in JavaScript with an example?

    By Chief Editor
  • Frontend Interview

    What is Symentic HTML?

    By Chief Editor

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor
  • Backend Interview

    Explain the useState and useEffect hooks in React

    By Chief Editor

    What is a Higher-Order Component (HOC) in React?

    By Chief Editor

    What is Virtual DOM in React?

    By Chief Editor

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor

    What is Callback Hell in JavaScript?

    By Chief Editor

    What is Position in CSS?

    By Chief Editor
  • Nodejs
  • JavaScript Interview
  • React Interview
  • Frontend Interview
  • Backend Interview
  • Contact Us
  • Advertise with Us
  • Complaint
  • Cookies Policy
  • Privacy Policy
  • Donate
  • 🔥
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • React Interview
  • Frontend Interview
  • HTML
  • CSS
  • Redux
  • Backend Interview
  • NodeJS
Font ResizerAa
Focus - Code RevealsFocus - Code Reveals
  • My Saves
  • My Interests
  • My Feed
  • History
  • Technology
Search
  • Homepage
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • HTML
  • CSS
  • Backend Interview
Have an existing account? Sign In
Follow US
© 2022 Code Reveals Inc. All Rights Reserved.
Home Blog 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 a Meta Tag in HTML?

A meta tag in HTML is an element that provides metadata (information) about a web…

By Chief Editor

What are controlled and uncontrolled components in React?

When working with form inputs in React, you will often hear the terms Controlled Components…

By Chief Editor

What is Block level Element and Inline Level Element?

Block-level Elements Block-level elements occupy the entire width of their parent container (by default) and…

By Chief Editor

You Might Also Like

Frontend InterviewJavaScript

Explain Deep Copy and Shallow Copy in JavaScript.

By Chief Editor
JavaScriptJavaScript Interview

What is memoization in JavaScript?

By Chief Editor
JavaScriptJavaScript Interview

Difference between document.createElement and document.createElementFragement in JavaScript?

By Chief Editor
CSSFrontend Interview

What is Position in CSS?

By Chief Editor

Focus by CodeReveals is your dedicated platform for mastering tech interviews and advancing your development skills. Whether you’re aiming to become a Frontend Developer, Backend Developer, or simply preparing for your next big interview, we’re here to guide you every step of the way.

Our mission is to help aspiring developers gain real-world knowledge, build confidence, and succeed in technical interviews. We offer structured content, curated interview questions, coding challenges, and practical guidance — all designed by experienced professionals who understand what top tech companies are looking for.

At Improve, we don’t just teach — we prepare you to think like a developer, solve like an engineer, and present like a pro.

Join us and start improving today — because your dream tech job is within reach

Most Famous
  • HTML
  • CSS
  • JavaScript
  • NodeJS
Top Categories
  • JavaScript Interview
  • React Interview
  • Frontend Interview
  • Backend Interview
Usefull Links
  • Contact Us
  • Advertise with Us
  • Complaint
  • Cookies Policy
  • Privacy Policy
  • Donate

©2025  Code Reveals Inc. All Rights Reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?