I understand them through examples. :)

First, lexical scope (also called static scope), in C-like syntax:

void fun()
{
    int x = 5;

    void fun2()
    {
        printf("%d", x);
    }
}

Every inner level can access its outer levels.

There is another way, called dynamic scope used by the first implementation of Lisp, again in a C-like syntax:

void fun()
{
    printf("%d", x);
}

void dummy1()
{
    int x = 5;

    fun();
}

void dummy2()
{
    int x = 10;

    fun();
}

Here fun can either access x in dummy1 or dummy2, or any x in any function that call fun with x declared in it.

dummy1();

will print 5,

dummy2();

will print 10.

The first one is called static because it can be deduced at compile-time, and the second is called dynamic because the outer scope is dynamic and depends on the chain call of the functions.

I find static scoping easier for the eye. Most languages went this way eventually, even Lisp (can do both, right?). Dynamic scoping is like passing references of all variables to the called function.

As an example of why the compiler can not deduce the outer dynamic scope of a function, consider our last example. If we write something like this:

if(/* some condition */)
    dummy1();
else
    dummy2();

The call chain depends on a run time condition. If it is true, then the call chain looks like:

dummy1 --> fun()

If the condition is false:

dummy2 --> fun()

The outer scope of fun in both cases is the caller plus the caller of the caller and so on.

Just to mention that the C language does not allow nested functions nor dynamic scoping.

Answer from Khaled Alshaya on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ lexical-scope-in-javascript
Lexical Scope in JavaScript - GeeksforGeeks
August 21, 2024 - Lexical scope defines the accessibility of variables and functions depending on their location in the source code.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ javascript-lexical-scope-tutorial
Lexical Scope in JavaScript โ€“ What Exactly Is Scope in JS?
August 19, 2021 - So this article will explain lexical scope by first examining the meaning of โ€œlexicalโ€ and โ€œscopeโ€. So, letโ€™s get it started by understanding the term โ€œscopeโ€. Scope refers to the area where an item (such as a function or variable) is visible and accessible to other code. ... Scope means area, space, or region. Global scope means global space or a public space. Local scope means a local region or a restricted region. ... // Define a variable in the global scope: const fullName = "Oluwatobi Sofela"; // Define nested functions: function profile() { function sayName() { function writeName() { return fullName; } return writeName(); } return sayName(); }
Discussions

javascript - What is lexical scope? - Stack Overflow
This is a good answer. But the question is tagged with JavaScript. Therefore I think this shouldn't be marked as the accepted answer. Lexical scope specifically in JS is different 2016-10-01T10:29:53.123Z+00:00 More on stackoverflow.com
๐ŸŒ stackoverflow.com
Lexical Scope in JavaScript - Stack Overflow
I am slightly confused as to how exactly scope works in JavaScript, mainly lexical scope. I understand that variables in global scope are accessible anywhere, and the only way to create a new scope... More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - what is the lexical scope of an object? - Stack Overflow
Object properties in a situation like your example are not part of any lexical scope. They are properties of an object, and they require a reference to the object in order to be accessed. ... I use "scope" instead of "LE" here, as the former is more common in JavaScript learning resources. More on stackoverflow.com
๐ŸŒ stackoverflow.com
What is lexical scope in JavaScript?
Lexical scope means "name resolution depends on the location in the source code where the named variable or function is defined." Contrasted with dynamic scope, where name resolution depends on program state. This SO answer has a good code sample of dynamic scope. In that dynamic scope sample, the name "x" in "func" can resolve to either of two "x" variables depending on who invoked "func". I honestly don't think I've ever worked in a language with dynamic scope, and I can't imagine I would want to. As the wiki article notes, even C has lexical scope, but there are two features that make JavaScript's name resolution more interesting than in C... nested functions and eval. Nested functions, of course, are things like: function outer() { let x = "Hello"; function inner() { console.log(x) } return inner; } In C, nested functions like this are forbidden, and that lets them keep the language implementation simple. They're able to store all local variables in a stack data structure. As we enter a function, local variables are pushed (allocated) on the stack, and as we leave a function, they're popped off. But if nested functions are not forbidden, if instead they're supported, like in JavaScript, then a stack data structure isn't good enough anymore. In the example above, the "inner" function is returned and outlives the "outer" function call, and since "inner" uses "x", that means "x" must also outlive the "outer" function call. That means popping off (deallocating) local variables such as "x" when we leave a function isn't an option anymore. The alternative to the stack is called the free store, or heap, and it's basically a big pool of memory. We can allocate and deallocate from it whenever we want, not just when we enter and leave a function. So in JavaScript, local variables such as "x" aren't stored on a stack, they're stored in the heap, and they'll stay there for as long as something still has a reference to them. But JavaScript doesn't store each variable in the heap individually. Instead, all of a function's local variables are allocated as a group, in an object named... drum roll... a lexical environment object. (Okay I simplified there just a pinch. Technically the variables are stored in an object called the environment record, and the environment record is stored in the lexical environment.) So, quick summary, lexical scope means as seen in source text. Lexical scope is normal and common across languages. Nested functions is the complication. Nested functions forces us to allocate locals in the heap rather than on the stack. We named the heap object of locals "lexical environment". More on reddit.com
๐ŸŒ r/learnjavascript
3
4
November 24, 2016
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ lexical-scope-in-javascript
Lexical scope in JavaScript
Lexical scope is the ability for a function scope to access variables from the parent scope. We call the child function to be lexically bound by that of the parent function. The diagram below outlines the supposed hierarchy that the lexical ...
๐ŸŒ
Medium
cleverzone.medium.com โ€บ lexical-scope-in-javascript-929789101dab
Lexical Scope in JavaScript. In JavaScript, lexical scope is theโ€ฆ | by Cleverzone | Medium
May 12, 2023 - Lexical Scope in JavaScript In JavaScript, lexical scope is the concept of determining the scope of a variable based on its declaration. This means that the scope of a variable is determined by the โ€ฆ
Top answer
1 of 16
812

I understand them through examples. :)

First, lexical scope (also called static scope), in C-like syntax:

void fun()
{
    int x = 5;

    void fun2()
    {
        printf("%d", x);
    }
}

Every inner level can access its outer levels.

There is another way, called dynamic scope used by the first implementation of Lisp, again in a C-like syntax:

void fun()
{
    printf("%d", x);
}

void dummy1()
{
    int x = 5;

    fun();
}

void dummy2()
{
    int x = 10;

    fun();
}

Here fun can either access x in dummy1 or dummy2, or any x in any function that call fun with x declared in it.

dummy1();

will print 5,

dummy2();

will print 10.

The first one is called static because it can be deduced at compile-time, and the second is called dynamic because the outer scope is dynamic and depends on the chain call of the functions.

I find static scoping easier for the eye. Most languages went this way eventually, even Lisp (can do both, right?). Dynamic scoping is like passing references of all variables to the called function.

As an example of why the compiler can not deduce the outer dynamic scope of a function, consider our last example. If we write something like this:

if(/* some condition */)
    dummy1();
else
    dummy2();

The call chain depends on a run time condition. If it is true, then the call chain looks like:

dummy1 --> fun()

If the condition is false:

dummy2 --> fun()

The outer scope of fun in both cases is the caller plus the caller of the caller and so on.

Just to mention that the C language does not allow nested functions nor dynamic scoping.

2 of 16
395

Lets try the shortest possible definition:

Lexical Scoping defines how variable names are resolved in nested functions: inner functions contain the scope of parent functions even if the parent function has returned.

That is all there is to it!

๐ŸŒ
DEV Community
dev.to โ€บ antonzo โ€บ lexical-scope-lexical-environment-execution-context-closure-in-javascript-5bn6
Lexical Scope, Lexical Environment, Execution Context, Closure in JavaScript - DEV Community
April 1, 2024 - Understanding these concepts allows ... in programming, particularly in JavaScript, refers to the context in which variables and functions are accessible or visible....
๐ŸŒ
Mozilla
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Guide โ€บ Closures
Closures - JavaScript - MDN Web Docs - Mozilla
The word lexical refers to the fact that lexical scoping uses the location where a variable is declared within the source code to determine where that variable is available. Nested functions have access to variables declared in their outer scope. Traditionally (before ES6), JavaScript variables ...
Find elsewhere
Top answer
1 of 4
5

To understand the lexical scope you need to have a basic understanding of the scope. In javascript, we have classified scope in three types

  1. Function scope
  2. Block Scope
  3. Lexical Scope

Function Scope -> The variables defined inside the function are considered in function scope.the var keyword is used to define the variable in the function scope.

Block scope -> The variables defined in the area within if,switch condition,for,and while loops. whenever you see '{}' curly braces, its a block. In Es6 const and let keywords allow developers to declare a variable in the block scope. which means those variables exist only within the corresponding block.

Copy   function animal(){
    if(true){
        var animal1 = "cat";
        const animal2 = "dog";
        let animal3 = "rat";
    }
    console.log(animal1);
    console,log(animal2); //animal2 is not defiend
    console,log(animal3); //animal3 is not defiend
}
animal();

result

cat

animal2 is not defined

animal3 is not defined

Lexical scope-> In 1 line I want to say "children scope have access to the variable in parent scope" `

Copyvar outerFunction = function()
{
    if(true){
        var x = 5;
        const y = 10;
    }

    var innerFunction = function(){
        if(true){
            alert(x);
            //alert(y);   //y is not defiend on line 13

        }
    }
    innerFunction();
}
 outerFunction();
 //console.log(x); // x is not defiend on line 20

`. The scope of a variable is defined by their position in the source code. In order to resolve variable javascript starts at the innermost scope and searches outward until it finds the variable it was looking for. lexical scoping is nice because we can easily figure out what the value of a variable will be by looking at the code; whereas in dynamic scoping the meaning of a variable can change at runtime by making it more difficult

2 of 4
2

Your understanding of how scope works for standard functions (including closures inside closures) is correct, but for arrow functions this statement is wrong:

"this" is bound to obj because of the lexical scoping of "this" with arrow functions.

With an arrow function this within the function is the same as whatever this was outside the function when it was created. It is not bound to obj in your example, but instead to whatever it was already bound to where that obj is being created.

It is useful in a situation such as:

Copythis.values.filter( x => x < this.max );

Inside that arrow function this is the same as it was outside the function. With a regular function it might have been written like this:

Copythis.values.filter( function ( x ) { return x < this.max }.bind( this ) );

or:

Copyvar self = this;
this.values.filter( function ( x ) { return x < self.max } );
๐ŸŒ
TechTarget
techtarget.com โ€บ whatis โ€บ definition โ€บ lexical-scoping-static-scoping
What Is Lexical Scoping?
JavaScript uses lexical scoping to resolve the variable names when a function is created inside another function. It determines the function's parent scope by looking at where the function was created instead of where it was invoked.
๐ŸŒ
SheCodes
shecodes.io โ€บ athena โ€บ 9740-what-is-lexical-scoping-and-how-does-it-work-in-javascript
[JavaScript] - What is Lexical Scoping and How Does it Work in JavaScript?
Lexical scoping is a way of defining how variables and functions interact based on their position within the code. Learn how your variables and functions interact with examples in JavaScript.
๐ŸŒ
Ghazi Khan
ghazikhan.in โ€บ blog โ€บ understanding-lexical-scope-javascript-guide
Understanding Lexical Scope in JavaScript: A Comprehensive Guide
Lexical scope refers to the set of rules that determines where and how a variable can be looked up in the nesting structure of functions. In simpler terms, it defines the accessibility and visibility of variables in a particular portion of your code.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ lexical-scope-in-javascript
Lexical Scope in JavaScript โ€“ Beginner's Guide
May 26, 2022 - Using the above intermediary code, it then starts the execution of the program. The process of determining the scopes of the variables/functions during runtime is called lexical scoping.
๐ŸŒ
DEV Community
dev.to โ€บ catherineisonline โ€บ what-is-lexical-scope-in-javascript-4gi8
What is Lexical Scope in JavaScript? - DEV Community
August 16, 2024 - In conclusion, lexical scope in JavaScript refers to the ability of an inner function to access variables and functions defined in its outer function or parent function, but not the other way around.
๐ŸŒ
CodeSweetly
codesweetly.com โ€บ javascript-lexical-scope
Lexical Scope in JavaScript โ€“ Explained with Examples | CodeSweetly
Lexical scope is the definition area of an expression. Therefore, an item's lexical scope is the place in which the item got created.
๐ŸŒ
Dreamer Infotech
dreamerinfotech.in โ€บ home โ€บ lexical scope in javascript โ€“ beginnerโ€™s guide
Lexical Scope in Javascript - Beginner's Guide
May 14, 2024 - Lexical scope, often referred to as static scope, is a scope determined at compile time-based on the programโ€™s structure. In simpler terms, it is about where a variable is declared in the code.
๐ŸŒ
Codesmith
codesmith.io โ€บ blog โ€บ understanding-javascript-scope
JavaScript Scope: Lexical, Block, and Hoisting Basics
August 18, 2025 - Scope defines how and where variables are accessed in JavaScript. JavaScript uses lexical scope, meaning scope is determined by a variable's location in the source code.
๐ŸŒ
Medium
medium.com โ€บ @catherineisonline โ€บ what-is-lexical-scope-in-javascript-708b1d1487ea
What is Lexical Scope in JavaScript? | by Ekaterine Mitagvaria | Medium
October 13, 2023 - In conclusion, lexical scope in JavaScript refers to the ability of an inner function to access variables and functions defined in its outer function or parent function, but not the other way around.
๐ŸŒ
Metana
metana.io โ€บ metana: coding bootcamp | software, web3 & cyber โ€บ full-stack โ€บ lexical scope in javascript - metana
Lexical Scope in JavaScript - Metana
March 4, 2025 - Lexical scope means a function remembers the environment (variables) where it was created. Whenever a function accesses a variable, JavaScript follows this lookup order: Local Scope โ€“ First, the function checks its own variables.
Top answer
1 of 2
2

I use "scope" instead of "LE" here, as the former is more common in JavaScript learning resources.

Functions have their own scope; objects do not. So while (1) the scope of stacy is indeed the global scope, (2) the code of sayName runs in its own scope, for which the parent scope is the global scope. this.name works because non-arrow functions have an additional binding this added to their scope, but name doesn't work because the object isn't a scope, it's just a data structure with a 'name' property. stacy.name would work, and you could declare a name variable with another var or let statement that would work in either scope.

2 of 2
0

Example 1:

var x = 20;

function foo() {
  console.log(x);
  console.log(this.x);
}

foo();

In the above example, if we do either console.log(x) or console.log(this.x), it will print the same value. Why so? Because the x is available as a variable to the function foo in its parent scope, it is also available as a property in the global object. The parent object for foo is the global object as it is defined and the function created using the function keyword has a bound context of its parent object by default. Now, look at the second example.

Example 2:

var stacy = {
  name: 'stacy',
  age: 33,
  sayName: function() {
    console.log(name);
    console.log(this.name);
    console.log(stacy.name);
  }
}

stacy.sayName();

In the second example, the console.log(name) and console.log(this.name) will not be equal but console.log(this.name) and console.log(stacy.name) will be equal as per above example's logic.

And the story will be different if use arrow function in place of the function keyword. In arrow function, this keyword refers to the global object.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ why-is-lexical-scoping-important
Why is Lexical Scoping Important ? - GeeksforGeeks
July 23, 2025 - Lexical scoping in JavaScript refers to the concept that a variable's scope is determined by the place in the code where it is declared. The variable's scope is fixed at the point of declaration, therefore it doesn't matter where the code is run.