DEV Community

Cover image for Higher-Order Functions and Callbacks: The Secret Sauce of JavaScript or Just Fancy Jargon?
Shubham Thakur
Shubham Thakur

Posted on

Higher-Order Functions and Callbacks: The Secret Sauce of JavaScript or Just Fancy Jargon?

Are you ready for a wild ride through the twisted and thrilling world of JavaScript? Today, we're about to dance with Higher-order functions and callbacks, which sound like they belong in a sci-fi movie rather than in your everyday JavaScript file. But don't panic! These are not aliens from another galaxy. They're here, they're handy, and they're about to become your best friends in coding. Let's get started, shall we?

"Higher-Order Functions" or "Fancy-Pants Functions"

First off, let's tackle this beast called Higher-Order Functions. No, they're not royalty, and they don't sit on a gilded throne. In JavaScript, a higher-order function is just a function that does one (or both) of these super cool things:

  1. Takes one or more functions as arguments (yeah, you read that right, functions inside functions, like a secret message in a spy novel)
  2. Returns a function as its result (like those surprise gifts that keep popping out of a box)

Here's a little example of a higher-order function. Meet greetSomeone. It takes a function as an argument (a callback, which we'll get to in a sec) and a name. It then calls the callback function with the name as an argument:

function greetSomeone(callback, name) {
  return callback(name);
}

const sayHello = name => `Hello, ${name}!`;

console.log(greetSomeone(sayHello, "John")); // Hello, John!
Enter fullscreen mode Exit fullscreen mode

In this code, greetSomeone is our higher-order function and sayHello is the callback function that we pass as an argument. Simple, right?

"Callbacks" or "Call Me Back, JavaScript!"

Okay, we've dealt with the high and mighty higher-order functions, now let's move onto callbacks. Callbacks are like that reliable friend who will always return your calls (or in this case, your functions).

A callback function is a function that is passed into another function as an argument and is expected to be called (or executed) later on. It's kind of like saying, "Hey JavaScript, do your thing, and when you're done, give me a ring!"

Let's see a simple example of a callback in action:

function bakeCake(callback) {
  console.log("Baking cake...");
  setTimeout(() => {
    callback();
  }, 2000);
}

function icingTheCake() {
  console.log("Icing the cake...");
}

bakeCake(icingTheCake);
Enter fullscreen mode Exit fullscreen mode

In this code, bakeCake is a function that takes a callback as an argument. Inside bakeCake, we're using JavaScript's setTimeout function (a built-in higher-order function, by the way) to simulate the time it takes to bake a cake. Once the cake is "baked" (after 2 seconds), the callback function icingTheCake is called, and we proceed to ice the cake.

Wrapping Up or "We Survived!"

So, there you have it, folks! Higher-order functions and callbacks aren't some big scary monsters. They're more like the secret superheroes of JavaScript, giving your code superpowers and making you look like a coding genius.

Remember, as with everything in coding (and in life), practice makes perfect. So, go ahead, play around with these concepts, and soon you'll be throwing around terms like 'higher-order function' and 'callback' as if they're going out of style.

Until next time, keep your code clean and your coffee

strong! Happy coding, my fellow JavaScript readers!

Top comments (2)

Collapse
 
rup1 profile image
Rup

ChatGPT? Is that you?? 🤣

Collapse
 
shubhamt619 profile image
Shubham Thakur

Yeah it's me ;)
How can I assist you today ?