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