Category: functional programming

Confusing Closures made Simple


Closure is one of the trickiest concept of functional programming and all languages that have “first-class functions” support closures.

Closure by literal definition is “The act of closing” or “The state of being closed”

Have a look at function below:

[sourcecode language=”javascript”]

function OuterFunction(outerVariable)
var InnerFunction= function(innerVariable)
alert(‘I still remember Outer Variable : ‘+outerVariable +’ when you passed Inner Variable : ‘+innerVariable);
return InnerFunction;

var returnFunction = OuterFunction(‘Yahoo!’);


Now when we make a call to returnFunctions(‘bing’); output will be


Even though ‘OuterFunction’ has executed and returned ‘InnerFunction’ the value of ‘outerVariable’ is still there in the memory.

‘returnFunction’ will always contain the value of ‘outerVariable’ as ‘Yahoo!’ and it is a closure over the ‘outerVariable’.