Skip to main content

Partial application

In short: Partial application is a technique in which a function is called not with all its arguments at once; instead, part of the arguments is "remembered", and a new function is returned that waits for the remaining arguments.


Detailed explanation

The idea: "fix" part of a function's parameters to get a new, more specific function.

Unlike currying, where a function takes one argument at a time, partial application lets you pass several at once.


Example 1 - a simple implementation

javascript
function multiply(a, b, c) { return a * b * c; } // Partially apply: fix a = 2 function partialMultiply(a) { return function(b, c) { return multiply(a, b, c); }; } const double = partialMultiply(2); console.log(double(3, 4)); // 24

We "fixed" the first argument (a = 2) and got a new function, double.


Example 2 - with bind()

JavaScript already lets you do partial application using Function.prototype.bind:

javascript
function add(a, b, c) { return a + b + c; } const add5 = add.bind(null, 5); // fix a = 5 console.log(add5(10, 20)); // 35

bind creates a new function with the arguments already substituted in.


Example 3 - a universal partial function

javascript
function partial(fn, ...fixedArgs) { return (...remainingArgs) => fn(...fixedArgs, ...remainingArgs); } function greet(greeting, name) { return `${greeting}, ${name}!`; } const sayHi = partial(greet, 'Hello'); console.log(sayHi('Alice')); // "Hello, Alice!"

Difference from currying

TechniqueWhat it does
CurryingTransforms a function with several arguments into a chain of one-argument functions (f(a)(b)(c))
Partial applicationSubstitutes part of the arguments and returns a new function (f(a, b, c) -> g(b, c))

Why partial application is needed

Reusing functions with different contexts Simplifying calls and configurations Improving readability Often used in functional programming, React and middleware


Summary

PropertyPartial application
Essence"Remembers" part of the arguments
ReturnsA new function
Exampleadd.bind(null, 5) or partial(add, 5)
Difference from curryingAccepts several arguments at once
BenefitCode reuse and simplification

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.