Introduction
In this tutorial, you will learn the concept of Currying in JavaScript.
Currying is a functional programming technique where a function with multiple arguments is transformed into a sequence of functions, and each function taking one argument at a time.
Normal Function:
add(2, 3, 4);
Currying Function:
add(2)(3)(4);
What is Currying?
Currying converts a function that takes multiple parameters into nested functions that take one parameter each.
Syntax:
function a(x) {
return function b(y) {
return function c(z) {
return x + y + z;
}
}
}
You can use this
a(2)(3)(4);
Currying with Arrow Functions
You will see currying with Arrow Function
console.log(add(1)(2)(3));
Output:
Why Use Currying?
Currying provides many benefits
1. Code Reusability
const multiply = a => b => a * b;
const double = multiply(2);
const triple = multiply(3);
console.log(double(4));
console.log(triple(4));
Output:
12
2. Better Function Composition
Currying works well with functional programming patterns used in libraries like React.
3. Partial Application
You can preset some arguments and reuse the function later.
Example:
const greet = greeting => name => greeting + " " + name;
const sayHello = greet("Hello");
console.log(sayHello("John"));
Output:
Currying vs Normal Function
| Feature | Normal Function | Curried Function |
|---|---|---|
| Arguments | Multiple arguments at once | One argument at a time |
| Function calls | Single call | Multiple nested calls |
| Reusability | Limited | High |
| Functional programming | Rare | Common |
Advantages of Currying
There are many advantages of Currying
1. Improves code reuse.
2. Enables partial application.
3. Works well with functional programming.
4. Makes functions more modular.
Real Life Example of Currying in JavaScript
1. Creating Reusable Discount Functions
Suppose you are working on an e-commerce website where different discount rates are applied to products. Instead of writing multiple functions, we can use currying to create reusable discount functions.
Example:
function discount(rate) {
return function(price) {
return price - (price * rate);
};
}
const tenPercentDiscount = discount(0.10);
const twentyPercentDiscount = discount(0.20);
console.log(tenPercentDiscount(100)); // 90
console.log(twentyPercentDiscount(100)); // 80
Output:
80
Real Scenario:
An online store may have different discount categories, like a festival discount or a membership discount. Currying helps create separate discount functions easily.
2. Custom Greeting Messages
Currying can help generate personalized messages.
Example:
function greet(greeting) {
return function(name) {
return greeting + ", " + name;
};
}
const sayHello = greet("Hello");
console.log(sayHello("John"));
Output:
Real Scenario:
You can use it in applications that generate custom messages for users, like notifications or emails.
Interview Questions
Q 1: What is currying in JavaScript?
Example:
function multiply(a) {
return function(b) {
return a * b;
};
}
console.log(multiply(2)(3)); // 6
Q 2: What will be the output? function add(a) {
return function(b) {
return a + b;
};
}
console.log(add(5)(3));
function add(a) {
return function(b) {
return a + b;
};
}
console.log(add(5)(3));
Explanation:
First call: add(5) returns a function
Second call: that function receives 3
Result = 5 + 3
Q 3: What will be the output?function greet(greeting) {
return function(name) {
return greeting + " " + name;
};
}
const hello = greet("Hello");
console.log(hello("Developer"));
function greet(greeting) {
return function(name) {
return greeting + " " + name;
};
}
const hello = greet("Hello");
console.log(hello("Developer"));
Q 4: What will be the output?function multiply(a) {
return function(b) {
return function(c) {
return a * b * c;
};
};
}
console.log(multiply(2)(3)(4));
function multiply(a) {
return function(b) {
return function(c) {
return a * b * c;
};
};
}
console.log(multiply(2)(3)(4));
Explanation:
The function takes arguments one by one and multiplies them.
Q 5: When should developers use currying?
1. Create reusable functions
2. Pre-set some arguments
3. Improve code readability
4. Build functional programming patterns
Conclusion
In JavaScript, Currying is a functional programming technique where a function takes arguments one at a time instead of all at once. Each function returns another function until all arguments are provided.
This approach helps developers create reusable, modular, and flexible functions. Currying is used to make code more maintainable and scalable.