Connect with us

closure in java script

Closures in javascript | Javascript lectures

Published

on

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

Join Our Groups For Latest Updates

Join Telegram Group [ 65,000 + Members ] Click Here
Join WhatsApp Group [Off campus drives] Click Here
Join WhatsApp Group [Internship drives] Click Here


Free courses with certifications

The post Closures in javascript | Javascript lectures appeared first on GeeksGod.

Click to comment

Leave a Reply

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

Newsletter

Trending

Copyright © 2019 - 2020