Today, we're diving into some cool ways to use functions in JS! π»β¨
1οΈβ£ Assign a Function to a Variable
function greeting() {
console.log("Good Morning");
}
let message = greeting;
message();
greeting(); // both will give the same results
Now you can call message()
just like greeting()
! π₯³
2οΈβ£ Pass Functions as Arguments
function greeting() {
return "Good morning";
}
function printMessage(anFunction) {
console.log(anFunction());
}
printMessage(greeting);
Functions as arguments? Yes, please! π
3οΈβ£ Return Functions from Other Functions
function greeting() {
return function() {
return "Good Morning!";
}
}
let anFunction = greeting();
let message = anFunction();
Functions returning functions? Mind-blowing! π€―
π Key Takeaway: Functions in JS are super flexible! Use them like any other variable. Get creative and have fun coding! π
Top comments (0)