Monday, August 3, 2020

Closures in Javascript

closure is the combination of a function and the scope object in which it was created. Closures let you save state

An example

function ageRelaxation(a) {
  return function(b) {
    return a + b;
  };
}
var boy2 = ageRelaxation(2);
var girl5 = ageRelaxation(5); boy2(18); // This results 20 girl5(20); // This results 25

No comments: