A higher order function is a function that takes one or more functions as arguments, or returns a function as its result.
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.
— MDN Web Docs
function greetings() {
var name = "Hello World!"; // name is a local variable created by greetings.
function displayGreetings() {
// displayGreetings() is the inner function, that forms the closure.
console.log(name); // use variable declared in the parent function
}
displayGreetings();
}
greetings();
Use the share button below if you liked it.
It makes me smile, when I see it.