function greeting(message) {
return function(name) {
console.log(`${message}, ${name}!`);
};
}
const sayHello = greeting("Hello");
sayHello("Alice"); // Output: "Hello, Alice!"
sayHello("Bob"); // Output: "Hello, Bob!"
const sayGoodbye = greeting("Goodbye");
sayGoodbye("Alice"); // Output: "Goodbye, Alice!"
Here, the greeting
function creates a closure that remembers the message
variable. This allows the inner function to use message
whenever it is called.