Skip to main content

Command Palette

Search for a command to run...

Lexical Environment in Javascript 🔥🔥

Published
•2 min read•View as Markdown
Lexical Environment in Javascript 🔥🔥

The lexical environment is an environment where identifier-variable mapping (here
identifier refers to the variable/functions name ) during the execution when we declared a variable/function. It doesn’t matter where we call it creates a lexical environment for the program. We can also refer as a combination of local memory and lexical parent (local memory is a portion of memory that is designated exclusively to a single unit. It may also describe memory specific to the single function or program ). To call a function if it doesn’t find the variable in his scope. It goes to the parent scope and if doesn’t find it in the parent scope then it goes to the next parent scope and so on until it finds the variable. If it isn’t fine the variable it gives the error name isn’t defined each child is connected to their parent through reference take a look at it.

var name = “Akash”
 function callName(){
  var a= 23;
  function callNo()
  {
     console.log(name)
     console.log(a)
  }
   callNo() 
} callName()

The output will be “Akash” 23 as expected. First encounter callName() function then it encounters callNo()It will search for the name variable in the local scope if it doesn’t find it and it goes to the parent scope if doesn’t find the parent scope it goes to the global scope then it's print to Akash same process to the a and its works only one way we can’t find the variable in the child scope. lexical addressing is useful to speed up the code, in the end, the lexical environment is nothing but An environment where stores the variable/functions during the execution and its one of the parts of the js engine

L

Backend bhi likh li tune iske 🤑