1. What are Closures?

Closures are the functions that refers to independent variables i.e variables that are used locally but defined in an enclosing scope. In other word a closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain.The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables.

Let’s take an example Demo

function makeFunc() {
	var name = "Jitendra";
  function displayName() {
  	alert(name);
  } 
  return displayName;
}
var myFunc = makeFunc();
myFunc();

This piece of code will alert the name “Jitendra” as expected. Interesting thing to notice in this code is makeFunc() returns a function displayeName() before it is executed. We might think once a function makeFunc() is executed, its local variable name cannot be accessed but it is not the case in case of Closure. A variable myFunc has become a closure. A closure is a special kind of object that combines two things: a function, and the environment in which that function was created. The environment consist of any local variables which were in-scope when the closure was created. In this case myFunc is a closure which consist of function displayName and the value of the local variable name.

Example 1

  function addNumber(a) {
  	return function(b) {
		return a + b;
	}
  }
  
  var add20 = addNumber(20);
  var add10 = addNumber(10);
  
  console.log("Using closure add20", add20(5)); //logs 25
  console.log("Using closure add10", add10(3)); //logs 13

Here add20() and add10() are closures as they share same function defination addNumber() but different environements. In add20()’s environment the value of a is 20 and for add10() the value of a is 10.