# Higher-Order Functions & Callbacks

> Learn how JavaScript treats functions as values that can be passed around, empowering incredibly flexible code.

Subject: JavaScript | Lesson 16 of 63 | 6 min read
Interactive lesson: https://codewithanimation.in/javascript/higher-order-functions
Previous: [The 'this' Keyword & Binding](https://codewithanimation.in/javascript/this-binding.md) | Next: [Recursion & the Call Stack](https://codewithanimation.in/javascript/recursion.md)

## Functions as First-Class Citizens

In JavaScript, functions are 'first-class citizens'. This means they are treated exactly like any other value (like a string or a number).

You can assign them to variables, store them in arrays, and most importantly: you can pass them as arguments to other functions.

## What is a Higher-Order Function?

A Higher-Order Function (HOF) is simply a function that does at least one of two things: it takes another function as an argument, or it returns a new function.

The function that gets passed *in* is called a 'Callback' function. The HOF 'calls back' to your function whenever it needs to.

## Why is this useful?

HOFs allow you to abstract away repetitive logic. Instead of writing a `for` loop every time you want to modify an array, you can use a HOF like `.map()`.

The `.map()` function handles the looping logic, and you just pass it a tiny callback function that tells it what to do with each individual item.

## Common mistake: Callback Hell

Historically, callbacks were heavily used for asynchronous code (like fetching data). Passing callbacks into callbacks into callbacks created deeply nested, unreadable code known as 'Callback Hell' or the 'Pyramid of Doom'. Modern JavaScript uses Promises and async/await to fix this.

## Tip: Don't invoke the callback!

When passing a callback, pass the *name* of the function (`setTimeout(greet, 1000)`), do NOT add parentheses (`setTimeout(greet(), 1000)`). Adding parentheses invokes it immediately and passes the *result* of the function instead of the function itself!

## Try it: The Callback Machine

Create a custom Higher-Order Function that loops over an array and applies whatever callback function you pass into it.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/higher-order-functions

## Check yourself

### Question 1

What makes a function a 'Higher-Order Function'?

- A. It accepts a function as an argument or returns a function.
- B. It runs faster than standard functions.
- C. It is declared at the very top of the global scope.

Correct answer: A

A Higher-Order Function (HOF) is defined as any function that operates on other functions, either by taking them in as arguments (callbacks) or by returning them.

- Why not B: Incorrect. It has nothing to do with performance.
- Why not C: Incorrect. The scope does not determine if it is higher-order.

### Question 2

What is a 'callback' function?

- A. A function passed into another function as an argument to be executed later.
- B. A function that makes a network request.
- C. A function that calls itself.

Correct answer: A

When you pass a function as an argument into a Higher-Order Function, that passed-in function is known as a callback.

- Why not B: Incorrect. Callbacks can be used for network requests, but that is not their definition.
- Why not C: Incorrect. A function that calls itself is called a 'recursive' function.

### Question 3

What is wrong with this code: `button.addEventListener('click', sayHi())`?

- A. It calls sayHi() immediately instead of waiting for the click.
- B. sayHi needs to be an arrow function.
- C. addEventListener does not accept callbacks.

Correct answer: A

By adding `()`, you are executing `sayHi` immediately as soon as the code loads, and passing its *return value* to `addEventListener`. You should pass the function reference itself: `sayHi`.

- Why not B: Incorrect. Standard functions work perfectly fine as callbacks.
- Why not C: Incorrect. addEventListener is one of the most common Higher-Order Functions in JavaScript!

## Remember this

- Functions are first-class citizens: they can be treated just like variables.
- A Higher-Order Function takes a function as an argument or returns one.
- A Callback is the function being passed in as an argument.
- When passing a callback, do not include parentheses `()` unless you want to execute it immediately.
