Closures in javascript | Javascript lectures

Share this Job Post



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 :

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

Join Our Groups For Latest Updates


Free courses with certifications



Share this Job Post

Leave a Reply

Your email address will not be published. Required fields are marked *