Skip to the solution
MediumPhone Screen8 min read

How do you implement infinite currying in JavaScript?

1.2k views

The problem

Question presented to candidate: "Write a function add such that add(1)(2)(3) eventually evaluates to 6 -- and the number of calls is not fixed, so add(1)(2)(3)(4)(5) should evaluate to 15. How would you implement that, and how does the runtime actually know when to stop chaining and produce a number?"

What a strong answer should cover:

  • Currying in general means turning a function that takes multiple arguments into a chain of functions that each take one argument (or a group), returning a new function until enough arguments have arrived.
  • Infinite currying cannot rely on counting a fixed number of calls, because there is no fixed arity -- instead, each call returns another callable function that also carries an overridden valueOf/toString, so the chain can be extended indefinitely.
  • The chain never "auto-terminates" by itself -- JavaScript only calls valueOf/toString when something coerces the returned function to a primitive, such as the + operator, a template literal, or JSON.stringify. Until that coercion happens, add(1)(2)(3) is still just a function sitting there.
  • An alternative, more explicit style avoids relying on implicit coercion entirely: return a callable object with a distinct terminator method, such as .call() or .done(), that the caller invokes explicitly to end the chain -- more verbose, but avoids the somewhat surprising, coercion-only quirk that a plain console.log(add(1)(2)(3)) prints a function, not a number.
  • Each intermediate function must close over the running total via a closure, so the accumulated sum persists across calls.

Clarifying questions expected:

  • "Should the terminator be implicit, through coercion like plus or a template literal, or should I add an explicit terminator method the caller has to call?"
  • "Does this need to support passing multiple arguments per call, like add(1, 2)(3), or strictly one argument per call?"

Code / implementation expected: Yes -- a real, executed implementation using the valueOf/toString coercion trick, plus a contrasting explicit-terminator version, both actually run and confirmed to produce correct sums for chains of varying length and grouping.

  • currying
  • closures
  • functional

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

8 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptInfinite currying via valueOf/toString coercion, plus an explicit-terminator alternative (run directly)

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 43 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What is the difference between Object.freeze, seal, and preventExtensions?

Continue