JavaScript Closure

In closure inner function can access the variable and parameters of its outer function even after the outer function has returned.In this example inner function can access the retirement function parameter and (A) variable which is outsite for inner function this is closure.

function retirment(retirmentAge){
	 var a = ' years left until retirement';
		return function(yearOfBirth){
			var age = 2016 - yearOfBirth;
			console.log(((retirmentAge - age) + a))
		}
	 }
	var retirmentUS = retirment(66);
		retirmentUS(1990);
retirment(66)(1991);

Leave a Reply

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