Closures in javascript | Javascript lectures

0
45
js logo

Closures in javascript

  • A closure is a function that has access to the parent scope.
  • It preserves the data from outside world.
  • In simple words: A closure is an inner function that has access to the outer (enclosing) function’s variables.

Types of Closure scopes

  1. Local scope (own scope)
  2. Outer function scope (one function within another)
  3. Global scope

Example Of Local Closure

var i = 10;

function Show() {
var j = 20;
document.write(j);
document.write(i);
}

Show();

Output :

  • 20
  • 10

In the above example ” i ” is accessible inside this function only.

Example Of Outer Function Scope

In outer function scope: Inner function local variables can access variables of parent function.

function Show() {

var j = "J"
document.write(j + "<br>");

function innerFun(){
var k = "K"
document.write(k + "<br>");
}
Show();

Amazon & Flipkart exciting Deals and Offers Click Here

Join Our Groups For Latest Updates

Join Telegram Group [70,000 + Members ]Click Here
Join WhatsApp Group [Off campus drives]Click Here
Join WhatsApp Group [Internship drives]Click Here
Join For Amazon and Flipkart DEALS & OFFERS Click Here

Free courses with certifications