Logo
HTMLCSSJavaScriptReactjsnewContactUpdates

Get started today

HTMLCSSJavaScriptReactjsnewContactUpdates

Tools

Resume BuilderQR GeneratorVS Code Editor

Connect

GitHubWhatsApp Channel
practice questions
closure

Lexical scope/Scope Chain

By Saket Bhatnagar•June 14, 2025•Beginner to Intermediate

Table of Contents

  1. lexical scope/Scope chain

lexical scope/Scope chain

  1. 1The ability of js engine to search for a variable in the outer scope when variable is not available in local scope is known as

    lexical scope or scope chain.

  2. 2It is ability of child to access variable from outside if its not present in local scope
  3. 3 Lexical scope : A function and global object.
    1let a = 10;
    2function test() {
    3 a++;
    4 console.log( a );
    5}
    6test();
    7
    8Output : 11

    When test function is executed js engine looks for ' a ' in local scope. Since it will not available it will look for a in outer scope that is global window object .
  4. 4Lexical scope : The child function and parent function with a help of closure.
    1function outer() {
    2 let a = 10;
    3 function inner() {
    4 console.log(a);
    5 }
    6 return inner;
    7}
    8
    9let res = outer();
    10res();
    11
    12Output : 10

    When the function inner is executed and console.log a is encountered, js engine looks for a in the local scope of function inner.
    Since, a is not present and function inner is child of function outer js engine will search for a in the parent function outer scope with the help of closure.

Share this article

Last updated: July 14, 2025

Join Our Community

Login to Join

© 2025 Saket Bhatnagar. All rights reserved.

    ☕