What is currying?

👨‍💻 Frontend Developer 🟠 May come up 🎚️ Medium
#JavaScript #Functions #JS Basics

Brief Answer

Currying is a technique of transforming a function with multiple parameters into a chain of functions with one parameter each. Each function in the chain takes one argument and returns the next function, until the final result is returned.

// Regular function
function add(a, b, c) {
  return a + b + c;
}
 
// Curried function
function curryAdd(a) {
  return function(b) {
    return function(c) {
      return a + b + c;
    };
  };
}
 
// Usage
console.log(add(1, 2, 3));        // 6
console.log(curryAdd(1)(2)(3));   // 6
console.log(curryAdd(1)(2));      // function (expects third argument)

Full Answer

Currying is like turning one big task into a chain of small tasks, where each next task depends on the previous one. Imagine assembling a LEGO constructor — first one piece, then another, and so on! 🧱

What is Currying

Currying is transforming a function so that instead of one call with all arguments, we can call it step by step, passing one argument at a time.

Simple Examples

Basic Currying Example

// Regular addition function
function sum(a, b) {
  return a + b;
}
 
// Curried version
function currySum(a) {
  return function(b) {
    return a + b;
  };
}
 
// Usage
console.log(sum(2, 3));      // 5
console.log(currySum(2)(3)); // 5

Currying with Three Parameters

// Regular function
function multiply(a, b, c) {
  return a * b * c;
}
 
// Curried version
function curryMultiply(a) {
  return function(b) {
    return function(c) {
      return a * b * c;
    };
  };
}
 
// Usage
console.log(multiply(2, 3, 4));        // 24
console.log(curryMultiply(2)(3)(4));   // 24

How Currying Works

Step-by-Step Execution

function add(a) {
  return function(b) {
    return a + b;
  };
}
 
const add5 = add(5);  // Returns function expecting b
const result = add5(3); // 8 (5 + 3)
 
// Or directly:
const result2 = add(5)(3); // 8

Creating Specialized Functions

function multiply(a) {
  return function(b) {
    return a * b;
  };
}
 
// Create specialized functions
const double = multiply(2);   // Double a number
const triple = multiply(3);   // Triple a number
 
console.log(double(5));  // 10
console.log(triple(5));  // 15

When to Use Currying

For Creating Reusable Functions

// Curried function for range checking
function inRange(min) {
  return function(max) {
    return function(value) {
      return value >= min && value <= max;
    };
  };
}
 
// Create specialized checks
const isChildAge = inRange(0)(12);     // For children 0-12 years
const isAdultAge = inRange(18)(100);   // For adults 18-100 years
 
console.log(isChildAge(8));   // true
console.log(isAdultAge(25));  // true

For Functional Programming

// Curried function for filtering arrays
function filterBy(property) {
  return function(value) {
    return function(array) {
      return array.filter(item => item[property] === value);
    };
  };
}
 
const users = [
  { name: 'John', age: 25 },
  { name: 'Peter', age: 30 },
  { name: 'Mary', age: 25 }
];
 
// Create filter for age 25
const filterByAge25 = filterBy('age')(25);
const result = filterByAge25(users);
// [{ name: 'John', age: 25 }, { name: 'Mary', age: 25 }]

Common Mistakes

Confusion with Syntax

function add(a) {
  return function(b) {
    return a + b;
  };
}
 
// ❌ Incorrect usage
// console.log(add(5)); // [Function] (not a number!)
 
// ✅ Correct usage
console.log(add(5)(3)); // 8

Forgetting About Intermediate Functions

function multiply(a) {
  return function(b) {
    return a * b;
  };
}
 
// ❌ Error
// const result = multiply(5, 3); // undefined
 
// ✅ Correct
const result = multiply(5)(3); // 15

Simple Rules

  1. Currying — splits function into chain of functions with one parameter each 🧱
  2. Step-by-step — each call returns next function in chain 🔗
  3. Specialization — allows creating partially applied functions 🎯
  4. Reusability — convenient for creating specialized versions of functions ♻️
  5. Functional programming — foundation of many functional techniques 🧠

Currying is a powerful technique that helps write more flexible and reusable code. Especially useful in functional programming! 💪


Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪