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)