Currying

Transform multi-argument functions into reusable chains of single-argument functions.

Lesson content

🎯 Goal By the end of this lesson, you'll understand: What is Currying? Why do we use it? How does it work? Real-world examples Interview questions What is Currying? Currying is the process of converting a function that takes multiple arguments into a sequence of functions, each taking one argument. Simple Definition: Instead of passing all arguments at once, pass one argument at a time. Normal Function A normal function takes all arguments together. function add ( a , b , c ) { return a + b + c ; } console . log( add ( 10 , 20 , 30 )); Output 60 Curried Function Now pass one argument at a time. function add ( a ) { return function ( b ) { return function ( c ) { return a + b + c ; }; }; } console . log( add ( 10 )( 20 )( 30 )); Output 60 How It Works add(10) │ ▼ Returns Function │ ▼ (20) │ ▼ Returns Function │ ▼ (30) │ ▼ Returns 60 Real-Life Example 🍕 Imagine ordering a pizza. Instead of saying everything at once: Pizza(Size, Crust, Topping) You decide step by step. Choose Size │ ▼ Choose Crust │ ▼ Choose Topping │ ▼ Order Ready Currying works the same way. Example 1 function multiply ( a ) { return function ( b ) { return a * b ; }; } const double = multiply ( 2 ); console . log( double ( 5 )); console . log( double ( 10 )); Output 10 20 Explanation multiply(2) │ ▼ Returns Function double(5) │ ▼ 10 double(10) │ ▼ 20 Notice that 2 is remembered using a closure . Example 2: Discount Calculator 🛒 function discount ( percent ) { return function ( price ) { return price - ( price * percent ) / 100 ; }; } const tenPercent = discount ( 10 ); console . log( tenPercent ( 1000 )); console . log( tenPercent ( 500 )); Output 900 450 Instead of passing the discount every time, create it once and reuse it. Example 3: Greeting function greet ( greeting ) { return function ( name ) { return greeting + ", " + name ; }; } const sayHello = greet ( "Hello" ); console . log( sayHello ( "Ajay" )); console . log( sayHello ( "Rahul" )); Output Hello, Ajay Hello, Rahul Arrow Function Version const add = a => b => c => a + b + c ; console . log( add ( 10 )( 20 )( 30 )); Output 60 Why Use Currying? Suppose you need to calculate GST for many products. Without Currying calculateGST ( 18 , 100 ); calculateGST ( 18 , 200 ); calculateGST ( 18 , 300 ); With Currying function calculateGST ( rate ) { return function ( price ) { return price + ( price * rate ) / 100 ; }; } const gst18 = calculateGST ( 18 ); console . log( gst18 ( 100 )); console . log( gst18 ( 200 )); console . log( gst18 ( 300 )); Output 118 236 354 You define the GST rate once and reuse it. Currying vs Normal Function Normal Function Curried Function Takes all arguments together Takes one argument at a time add(1,2,3) add(1)(2)(3) Less flexible More reusable Currying vs Closure Currying Closure Converts multiple arguments into single-argument functions Remembers variables from the outer function Often uses closures internally A language feature used by currying Interview Question What is the output? function multiply ( a ) { return function ( b ) { return a * b ; }; } const triple = multiply ( 3 ); console . log( triple ( 4 )); Output 12 Reason: multiply(3) returns a function that remembers a = 3 . When triple(4) is called, it calculates 3 × 4 = 12 . Where is Currying Used? ✅ Functional Programming ✅ React Event Handlers ✅ Redux ✅ Express Middleware ✅ Utility Libraries (Lodash, Ramda) Key Points ✅ Currying converts a function with multiple arguments into a chain of single-argument functions. ✅ It makes functions more reusable and configurable. ✅ Currying relies on closures to remember previously supplied arguments. ✅ You call curried functions one argument at a time. 🧠 Easy Formula Normal Function add(a, b, c) │ ▼ Curried Function add(a)(b)(c) Remember: Normal Function = All arguments together Curried Function = One argument at a time 🚀