Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Currying Advanced in JavaScript

Reuse price and quantity in JavaScript with curry3 and chained single-argument calls. Add delivery in the final call; the helper requires exactly three inputs.

AdvancedAdvanced JavaScriptExample 4 of 12

Run this example once in a fresh browser console. The console shows the printed results.

currying-advanced.js
1// This helper is for exactly three required arguments.
2function curry3(fn) {
3 return a => b => c => fn(a, b, c);
4}
5const lineTotal = curry3((price, quantity, delivery) => price * quantity + delivery);
6const notebookTotal = lineTotal(40);
7const twoNotebooks = notebookTotal(2);
8console.log('Pickup:', twoNotebooks(0));
9console.log('Delivered:', twoNotebooks(15));
10console.log('One pen:', lineTotal(10)(1)(0));

Sample Output

Pickup: 80; Delivered: 95

Both final calls reuse price 40 and quantity 2, then add delivery 0 or 15.

One pen: 10

A fresh three-stage call uses price 10, quantity 1, and delivery 0.

In this one

  • Break a three-argument function into stages
  • Reuse a partially supplied calculation
  • Trace remembered arguments
  • Recognize the limits of a small curry helper

How it works

  1. 1

    Define curry3 to return three nested one-argument functions.

  2. 2

    Wrap price times quantity plus delivery.

  3. 3

    Supply price 40 to create a notebook-specific function.

  4. 4

    Supply quantity 2, then reuse that function with delivery 0 and 15.

  5. 5

    Compare Pickup 80, Delivered 95, and the independent pen total 10.

Edge cases

Wrong call shape

lineTotal(40, 2, 0) does not finish this curried function.

This helper expects three separate calls: lineTotal(40)(2)(0). JavaScript ignores extra arguments in the first arrow function.

Optional arguments

A general curry helper based on function length can surprise beginners.

Keep this version explicit: it is only for exactly three required arguments. Do not present it as a universal wrapper.

What's going on

Currying changes a function that takes several arguments into a series of functions that each take one argument. It lets you supply a value now and reuse the remaining steps later. This stationery example starts with price, then quantity, then delivery cost.

curry3 is deliberately limited to three required arguments. Calling lineTotal(40) returns a function that remembers price 40. Calling that returned function with 2 remembers the quantity too. The final call supplies delivery and runs the original calculation, giving 80 for pickup or 95 with delivery.

The remembered values are kept by closures. A closure is a function that keeps access to variables from where it was created. This pattern is helpful when the same starting values are reused, but a normal three-argument function may be clearer for a one-off calculation. This helper does not automatically handle optional parameters, a changing argument count, or functions that depend on a calling object's this value.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What does twoNotebooks(15) return?

Pick an answer to check it.

Question 2

How many stages does curry3 create?

Pick an answer to check it.

Questions people ask

Why does lineTotal(40) not return a number?

Only the price is known. It returns the next function, which still needs quantity and delivery.

Can twoNotebooks be used more than once?

Yes. Its saved price and quantity are reused, while each final call supplies a delivery charge.

Is currying always simpler?

No. Use it when staged reuse helps. A direct function call is often easier for a single calculation.