Currified function that multiples array elements in JavaScript

Problem

We need to write a JavaScript function that takes an array and returns another function. This returned function takes a number and produces a new array where each element is the product of the corresponding element from the original array and the number.

What is a Curried Function?

A curried function breaks down a function that takes multiple arguments into a series of functions that each take a single argument. In this case, instead of multiply(array, number), we have multiply(array)(number).

Example

Following is the code ?

const arr = [2, 5, 2, 7, 8, 4];
const num = 4;

const produceWith = (arr = []) => (num) => {
    const res = arr.map(el => {
        return el * num;
    });
    return res;
};

console.log(produceWith(arr)(num));

Output

Following is the console output ?

[ 8, 20, 8, 28, 32, 16 ]

Simplified Version

We can make the function more concise using arrow function syntax:

const arr = [3, 6, 9, 12];
const multiplier = 3;

const multiplyWith = (arr = []) => (num) => arr.map(el => el * num);

console.log(multiplyWith(arr)(multiplier));
[ 9, 18, 27, 36 ]

How It Works

The function works in two steps:

  1. produceWith(arr) returns a new function that "remembers" the array
  2. Calling the returned function with a number multiplies each array element by that number

Practical Use Case

You can create reusable multiplier functions:

const prices = [10, 25, 50, 100];
const applyTax = produceWith(prices);

console.log("With 10% tax:", applyTax(1.1));
console.log("With 15% tax:", applyTax(1.15));
With 10% tax: [ 11, 27.5, 55, 110 ]
With 15% tax: [ 11.5, 28.75, 57.5, 115 ]

Conclusion

Curried functions provide flexibility by allowing partial application. This pattern is useful when you need to apply the same operation with different parameters to the same dataset.

Updated on: 2026-03-15T23:19:00+05:30

261 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements