DEV Community

Aditya Singh
Aditya Singh

Posted on

🚀 JavaScript Functions

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

Enter fullscreen mode Exit fullscreen mode

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);

Enter fullscreen mode Exit fullscreen mode

Functions as arguments? Yes, please! 🎁

3️⃣ Return Functions from Other Functions

function greeting() {
    return function() {
            return "Good Morning!";
        }
    }

    let anFunction = greeting();
    let message = anFunction();
Enter fullscreen mode Exit fullscreen mode

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)