0Pricing
JavaScript Academy · Lesson

Currying Functions

Transform multi-argument functions into chains.

Currying Functions is a free JavaScript Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is Currying?

Currying transforms a function that takes many arguments into a sequence of functions that each take one argument.

Instead of add(1, 2, 3) you call add(1)(2)(3). Each call returns the next function until all arguments are collected.

Currying By Hand

The clearest way to see currying is to write nested functions, each returning the next one and closing over the earlier arguments.

function add(a) {
  return function (b) {
    return function (c) {
      return a + b + c;
    };
  };
}

console.log(add(1)(2)(3));

Nested Arrow Returns

Arrow functions make curried definitions shorter. Each => returns the next arrow, and the final one does the work.

const add = a => b => c => a + b + c;

console.log(add(1)(2)(3));
console.log(add(10)(20)(30));

Closures Remember Arguments

Currying relies on closures. Each returned function keeps a reference to the variables from its outer scope, so earlier arguments stay available until the last call runs.

const greet = greeting => name => greeting + ', ' + name + '!';

const sayHi = greet('Hi');
console.log(sayHi('Ada'));
console.log(sayHi('Grace'));

Calling Step By Step

You do not have to apply all arguments at once. You can stop midway and keep the partial result in a variable for reuse.

const add = a => b => c => a + b + c;

const addOne = add(1);
const addOneAndTwo = addOne(2);

console.log(addOneAndTwo(3));
console.log(addOneAndTwo(100));

A Generic curry Helper

Writing nested functions by hand is tedious. A generic curry helper collects arguments until it has enough, then calls the original function.

function curry(fn) {
  return function curried(...args) {
    if (args.length >= fn.length) {
      return fn(...args);
    }
    return (...more) => curried(...args, ...more);
  };
}

const add = curry((a, b, c) => a + b + c);
console.log(add(1)(2)(3));

Flexible Calling With curry

A good curry helper accepts arguments in any grouping. fn.length tells it how many parameters the original function declared.

function curry(fn) {
  return function curried(...args) {
    if (args.length >= fn.length) {
      return fn(...args);
    }
    return (...more) => curried(...args, ...more);
  };
}

const add = curry((a, b, c) => a + b + c);
console.log(add(1, 2, 3));
console.log(add(1, 2)(3));
console.log(add(1)(2, 3));

Currying For Specialization

Currying makes specialization natural: apply the first argument and reuse the returned function across many calls.

const multiply = a => b => a * b;

const double = multiply(2);
const triple = multiply(3);

console.log(double(5));
console.log(triple(5));

Currying With Array Methods

Curried functions slot perfectly into map and filter because each produces a one-argument function ready to receive each element.

const greaterThan = min => value => value > min;

const over10 = greaterThan(10);
console.log([5, 12, 8, 20].filter(over10));

Currying vs Partial Application

Partial application fixes some arguments and leaves the rest for one later call. Currying always breaks a function into a chain of single-argument calls.

Currying is one specific, fully-decomposed form of partial application.

const partial = (a, b) => c => a + b + c;
const curried = a => b => c => a + b + c;

console.log(partial(1, 2)(3));
console.log(curried(1)(2)(3));

Why Curry?

Currying encourages small, composable, reusable functions. It pairs naturally with function composition, which you will explore next. The key insight: a curried function is never 'incomplete', it simply returns another function until satisfied.

const tag = name => content => '<' + name + '>' + content + '</' + name + '>';

const bold = tag('b');
console.log(bold('important'));
console.log(tag('i')('slanted'));

Quick Check

Given const add = a => b => c => a + b + c;, what does add(1)(2) evaluate to?

Recap: Currying

You learned to decompose functions into single-argument chains:

  • Curried functions return a new function per argument via closures.
  • Nested arrows give compact curried definitions.
  • A generic curry helper uses fn.length to know when enough arguments arrived.
  • Currying enables clean specialization and composition.

Frequently asked questions

Is the “Currying Functions” lesson free?

Yes — the full text of “Currying Functions” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Currying Functions”?

Transform multi-argument functions into chains. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Currying Functions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Partial Application
  2. Currying Functions
  3. Function Composition
  4. Building pipe and compose
← Back to JavaScript Academy