Dark mode switch icon Light mode switch icon

Scope/Lexical environment in javascript

1 min read

Scope is directly depended on the lexical environment of a variable.

Lexical means presence in hierarchy. Lexical environment is created everytime a execution context is created.

Lexical environment is the local memory environment and the lexical environment of the parent.

  function a()  {
    console.log(b); // prints 10
  }
  var b = 10;
  a();
  function a()  {
    var b = 10;
    c();
    function c() {
      console.log(b) // prints 10
    }
  }
  a();

Originally published on by Rakshith Bellare