JavaScript Notes
Learn JavaScript currying with step-by-step examples, closures, partial application, real-world use cases, common mistakes, and top interview Q&A.
What is Currying?
Currying is a functional programming technique where a function that takes multiple arguments is transformed into a sequence of functions, each taking one argument at a time.
One-liner: Currying turnsf(a, b, c)intof(a)(b)(c).
A curried function doesn't run until all its arguments are provided. Each intermediate call returns a new function waiting for the next argument — powered by closures.
Why Use Currying?
- Reusability — create specialized functions from general ones
- Composability — chain functions in a pipeline
- Partial application — pre-fill some arguments, supply rest later
- Readability — describe transformations as a chain
Basic Currying Example
Without Currying
function add(a, b) {
return a + b;
}
console.log(add(3, 5));8
With Currying
function add(a) {
return function(b) {
return a + b;
};
}
const addThree = add(3); // Returns a function with a=3 remembered
console.log(addThree(5)); // Supplies b=5
console.log(add(3)(5)); // Direct chain call8 8
Step-by-Step Execution Trace
Closure Visualization
Arrow Function Currying
Arrow functions make curried functions extremely concise:
10 15 24
Three-Level Currying
Hello, Mr. Sharma! Hello, Mr. Verma! Hi, Ms. Patel!
Real World Use Case 1 — Configurable Logger
[ERROR] [Auth] Invalid token [ERROR] [Auth] Session expired [WARN] [Database] Connection slow
Real World Use Case 2 — Discount Calculator
450 180 800
Real World Use Case 3 — URL Builder
https://api.wohotech.com/api/v2/users https://api.wohotech.com/api/v2/posts https://api.wohotech.com/api/v2/comments
Generic curry() Implementation
function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) {
// All arguments supplied — call the original function
return fn(...args);
}
// Return a function collecting more args
return function(...moreArgs) {
return curried(...args, ...moreArgs);
};
};
}
// Usage
function add(a, b, c) {
return a + b + c;
}
const curriedAdd = curry(add);
console.log(curriedAdd(1)(2)(3));
console.log(curriedAdd(1, 2)(3));
console.log(curriedAdd(1)(2, 3));
console.log(curriedAdd(1, 2, 3));6 6 6 6
How the generic curry() works:
Infinite Currying (Add Forever)
function infiniteAdd(a) {
return function(b) {
if (b === undefined) return a;
return infiniteAdd(a + b);
};
}
console.log(infiniteAdd(1)(2)(3)());
console.log(infiniteAdd(5)(10)(15)());6 30
Call with no argument to get the accumulated result.
Currying vs Partial Application
These are related but different concepts:
| Feature | Currying | Partial Application |
|---|---|---|
| Splits into | One argument at a time | Any number of arguments |
| Implementation | Nested functions | bind() or custom |
| Fixed structure | Yes — f(a)(b)(c) | Flexible |
| Example | add(1)(2) | add.bind(null, 1)(2) |
// Partial application with bind
function add(a, b) { return a + b; }
const add5 = add.bind(null, 5); // Pre-fill a=5
console.log(add5(3)); // b=3 → 88
Common Mistakes
❌ Mistake 1 — Calling Curried Function All At Once
[Function (anonymous)]
// CORRECT
console.log(multiply(3)(4));12
❌ Mistake 2 — Expecting Infinite Curry to Resolve Automatically
❌ Mistake 3 — Confusing Currying with Closure
Currying uses closures, but they aren't the same thing:
- Closure: A function that remembers its outer scope
- Currying: A technique of breaking multi-arg functions into chains of single-arg functions
All curried functions use closures, but not all closures are currying.
Currying in Practice — Redux Middleware
This three-level curried function is the signature of every Redux middleware.
Interview Questions 🎯
Q1. What is currying in JavaScript?
Currying is a technique where a function with multiple parameters is transformed into a sequence of functions, each accepting one argument at a time. f(a, b, c) becomes f(a)(b)(c). The intermediate functions hold the already-received arguments via closures.
Q2. How does currying use closures?
When the first argument is passed, the outer function stores it in its local scope and returns an inner function. The inner function has access to the outer scope via closure, so it can reference the first argument when the next argument arrives. This chain continues for each argument.
Q3. What is the difference between currying and partial application?
- Currying: Always one argument per call — strict
f(a)(b)(c)structure - Partial application: Pre-fill any number of arguments at once — more flexible
Currying is a specific form of partial application.
Q4. Write a curried function to add three numbers.
6
Q5. Write a generic curry() function.
Q6. What is infinite currying?
A currying pattern where calling with no argument returns the accumulated result, while calling with an argument continues adding:
function add(a) {
return function(b) {
if (b === undefined) return a;
return add(a + b);
};
}
console.log(add(1)(2)(3)()); // 6Q7. Where is currying used in real JavaScript?
- Redux middleware:
(store) => (next) => (action) => {} - Lodash/Ramda:
_.curry(fn) - React HOCs:
connect(mapState)(Component) - Event handlers with configuration:
handler(config)(event) - URL builders, loggers, validators with configurable parameters
Q8. Does currying affect performance?
Currying adds overhead because each call creates a new function object (closure). For performance-critical hot paths, regular function calls are faster. But for code organization, reusability, and composition, the overhead is negligible.
Key Takeaways 🔑
- Currying transforms
f(a, b, c)→f(a)(b)(c)using closures - Each intermediate call returns a new function waiting for the next argument
- The original function only runs when all arguments are supplied
- Enables partial application — specialize functions by pre-filling arguments
- Arrow functions make currying very concise:
(a) => (b) => a + b - A generic
curry()utility usesfn.lengthto know when all args are ready - Widely used in Redux, React HOCs, functional libraries (Lodash, Ramda)
Practice Problems
- Write a curried function
multiply(a)(b)(c)that multiplies three numbers. - Create a curried
filter(predicate)(array)function. - Implement the generic
curry(fn)utility from scratch. - Write a curried URL builder for
buildUrl(protocol)(domain)(path). - Implement infinite currying
sum(1)(2)(3)(4)()that adds until called with no args.
Summary
Currying is a functional programming technique that transforms a multi-argument function into a chain of single-argument functions. Each call returns a new function (via closure) that waits for the next argument — the original function only executes once all arguments are collected. Currying enables code reuse (create specialized functions from general ones), function composition, and partial application. In JavaScript, it's implemented naturally through closures, is extremely concise with arrow functions, and is widely used in Redux middleware, React patterns, and functional libraries like Lodash and Ramda.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Currying - Complete Guide with Examples.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, advanced, currying, javascript currying - complete guide with examples
Related JavaScript Master Course Topics