// Function declaration
function greet(name) {
return "Hello, " + name + "!";
}
// Assigning a function to a variable
var sayHello = greet;
// Using the function through the variable
console.log(sayHello("Tom")); // Output: Hello, Tom!
// Passing a function as an argument to another function
function executeFunction(func) {
return func("Jerry");
}
console.log(executeFunction(greet)); // Output: Hello, Jerry!
// Returning a function from another function
function createGreetingFunction() {
return function(name) {
return "Hola, " + name + "!";
};
}
var spanishGreet = createGreetingFunction();
console.log(spanishGreet("Xavier")); // Output: Hola, Xavier!