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)