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
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)); // 24We "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:
function add(a, b, c) {
return a + b + c;
}
const add5 = add.bind(null, 5); // fix a = 5
console.log(add5(10, 20)); // 35bind creates a new function with the arguments already substituted in.
Example 3 - a universal partial function
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
| Technique | What it does |
|---|---|
| Currying | Transforms a function with several arguments into a chain of one-argument functions (f(a)(b)(c)) |
| Partial application | Substitutes 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
| Property | Partial application |
|---|---|
| Essence | "Remembers" part of the arguments |
| Returns | A new function |
| Example | add.bind(null, 5) or partial(add, 5) |
| Difference from currying | Accepts several arguments at once |
| Benefit | Code reuse and simplification |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.