JavaScript · Closures · Mid-Level · Live Coding
Implement a simple `partial` or `multiplyBy` using closures. What interview signal does this show?
Short Interview Answer
Return an inner function that remembers preset arguments from the outer scope — demonstrates closures and functional patterns.
Detailed Explanation
Partial application fixes some arguments and returns a function awaiting the rest. Closures store the preset args without needing a class. This differs slightly from currying (unary chain), but both rely on closures. In interviews, a clean implementation shows you can design APIs that configure behavior once and reuse it. Discuss trade-offs: readability vs. bind, and when explicit objects are clearer than nested closures.
Example
function multiplyBy(factor) {
return (n) => n * factor;
}
const double = multiplyBy(2);
double(5); // 10
function partial(fn, ...preset) {
return (...rest) => fn(...preset, ...rest);
}Interview Tip
Write it live, then name the pattern (partial application).
Common Mistake
Confusing partial application with currying — related but not identical.