Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

Explain closure in JavaScript.

user-image
Question added by Rizwan Shah , Project Manager , COMSATS IIT
Date Posted: 2014/09/30
Hassan Naqvi
by Hassan Naqvi , Manager Web Portal , Innovative Solutions

A closer is a function inside a function that access a variable outside its scope. It is important to know a closure may or may not return a value.

 

Example:

=======

function count() {

    var i =0;  // name is a local variable created by init

    function countNum() {    // countNum() is a function inside a function, A Closure 

        i +=1;  // 'i' is a variable declared in the parent function outside countNum()   

    }

    countNum();    

}

count();

More Questions Like This