Skip to solution
mediumPhone Screen

How do you implement infinite currying in JavaScript?

1.2k views
01

Understand 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.

curryingclosuresfunctional
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript closures and functional-programming interview questions. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every result below was actually run on Node.js v24.19.0 —

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

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

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 43 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track