DEV Community

Cover image for Javascript Essentials 101: Higher-Order Functions, Currying, and Partial Application
João Pedro
João Pedro

Posted on

Javascript Essentials 101: Higher-Order Functions, Currying, and Partial Application

JavaScript Essentials 101: Higher-Order Functions, Currying, and Partial Application

Introduction

Welcome to JavaScript Essentials 101, a series created to uncover the core fundamentals of JavaScript that every developer should know. We’re not focusing on trendy features but on foundational concepts—techniques and structures that have been part of JavaScript from the start and are essential for writing clean, effective code. Even if these concepts sound advanced, think of them as part of JavaScript’s core DNA, tools that will enhance your problem-solving ability, improve your interview performance, and simplify code maintenance.

Why This Matters

You may not have heard of higher-order functions, currying, or partial application, but they’re essential concepts that enable more efficient, reusable, and scalable code. Whether you're working with data transformations, optimizing function calls, or handling complex logic, these tools are key to understanding JavaScript’s functional side.

Let's dive into these techniques with examples that show how practical and powerful they are.


Higher-Order Functions

A higher-order function is any function that either takes another function as an argument or returns a function as its result. These are integral to JavaScript since they enable functional programming techniques, making data manipulation cleaner and more intuitive.

Example: Let’s look at map, filter, and reduce, three higher-order functions that transform arrays with concise syntax:

const numbers = [1, 2, 3, 4, 5];

// Using map to double each number
const doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6, 8, 10]

// Using filter to get even numbers
const evens = numbers.filter(num => num % 2 === 0);
console.log(evens); // [2, 4]

// Using reduce to sum up all numbers
const sum = numbers.reduce((acc, num) => acc + num, 0);
console.log(sum); // 15
Enter fullscreen mode Exit fullscreen mode

Currying

Currying transforms a function with multiple arguments into a sequence of functions, each taking a single argument. It’s powerful for breaking down complex operations into manageable parts, making your functions more reusable.

Example: Here’s a curried addition function:

const add = a => b => a + b;
const addFive = add(5);

console.log(addFive(3)); // 8
console.log(addFive(10)); // 15
Enter fullscreen mode Exit fullscreen mode

With currying, we create a reusable addFive function from add. This modular approach allows you to use addFive anywhere in your code where that specific calculation is needed.

Partial Application

Partial application is like currying, but it lets you pre-fill some of a function’s arguments while keeping the rest dynamic. This can be a game-changer for repetitive tasks with fixed parameters.

Example: Let’s apply a 20% tax rate to different prices:

const applyTax = (rate, price) => price * (1 + rate);
const applyVAT = applyTax.bind(null, 0.2); // Fixed tax rate of 20%

console.log(applyVAT(100)); // 120
console.log(applyVAT(200)); // 240
Enter fullscreen mode Exit fullscreen mode

Here, applyVAT allows you to quickly calculate prices with a fixed tax rate, keeping the code streamlined and readable.


Conclusion

These "advanced" functions might seem unfamiliar, but they’re essential tools for mastering JavaScript’s capabilities. By understanding higher-order functions, currying, and partial application, you’ll write cleaner, more efficient code that’s easier to test and maintain. In the next chapter, we’ll continue building on these concepts, gradually unlocking JavaScript’s full potential. Keep at it—you’re investing in the foundations that will elevate your skills and confidence as a developer.

Top comments (0)