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)