Closures

by Daman Daman

JavaScript

var sayHello = function (name) {
  var text = 'Hello, ' + name;
  return function () {
    console.log(text);
  };
};

sayHello('Todd');  // nothing happens, no errors, just silence...

var helloTodd = sayHello('Todd');
helloTodd();       // will call the closure and log 'Hello, Todd'

sayHello('Bob')(); // calls the inner returned function without assignment