Lexical Scope in JavaScript ๐Ÿ™‚

Koushik SahaKoushik Saha
1 min read

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 ability of inner function to access outer function element or parent element is known as lexical scope.

See Example Below :

let firstName = "Koushik";
function outer() {
 console.log(firstName); // output : Koushik
}
outer();
function outer() {
  let firstName = "Koushik";
  function inner() {
    console.log(firstName); //output : Koushik
  }
inner();
}
outer();
let lastName = "Saha";
function outer() {
  let firstName = "Koushik";
  function inner() {
    console.log(firstName + lastName); //output : Koushik Saha
  }
inner();
}
outer();
1
Subscribe to my newsletter

Read articles from Koushik Saha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Koushik Saha
Koushik Saha

A Good Learner. And Web Developer.