What are higher-order functions in JavaScript?

Understand higher-order functions — functions that take or return other functions for composable code.

intermediateIntermediate conceptsjavascripthigher-order functionsfunctional programming
Published: 11/3/2025
Updated: 11/3/2025

Question

What are higher-order functions in JavaScript?


Answer

A higher-order function either:

  • Takes one or more functions as arguments, or
  • Returns a function as a result.
function withLogging(fn) {
  return function (...args) {
    console.log('Calling with args:', args);
    return fn(...args);
  };
}

const add = (a, b) => a + b;
const loggedAdd = withLogging(add);
console.log(loggedAdd(2, 3));

Real-World Example

React’s useEffect, Array.map, and Redux’s connect() are all built on higher-order principles.

Quick Practice

Write a function times(n, fn) that calls fn exactly n times.

Summary

Higher-order functions make code modular, reusable, and expressive.

Related Videos
Watch these videos to learn more about this topic
Frequently Asked Questions

Are all array methods higher-order functions?

Yes, methods like map, filter, reduce, forEach, etc., are all higher-order functions.


Stay Updated

Get the latest frontend challenges, interview questions and tutorials delivered to your inbox.