Skip to main content

Command Palette

Search for a command to run...

Lexical Scope in JavaScript ๐Ÿ™‚

Published
โ€ข1 min readโ€ขView as Markdown
K

A Good Learner. And Web Developer.

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();