Function factory
function factory is a function that returns other functions. That is, it "produces" new functions based on given parameters, hence the name "factory".
Definition in simple terms
A function factory is a function that, when called, creates and returns a new function, often with specific behavior based on the arguments passed to it.
It is used for creating closures and reusing logic.
Example 1 - a simple greeter factory
javascript
function createGreeter(greeting) {
return function(name) {
console.log(`${greeting}, ${name}!`);
};
}
const sayHi = createGreeter('Hello');
const sayBye = createGreeter('Bye');
sayHi('Alice'); // Hello, Alice!
sayBye('Maria'); // Bye, Maria!Here:
createGreeteris the factory;sayHiandsayByeare functions "produced" by this factory;- Each new function closes over its own value of
greeting.
Example 2 - a counter factory
javascript
function createCounter(start = 0) {
let count = start;
return function() {
count++;
return count;
};
}
const counterA = createCounter();
const counterB = createCounter(10);
console.log(counterA()); // 1
console.log(counterA()); // 2
console.log(counterB()); // 11Each counter function has its own closure and keeps independent state.
Example 3 - a filter factory
javascript
function createFilter(minValue) {
return function(numbers) {
return numbers.filter(n => n >= minValue);
};
}
const filterAbove10 = createFilter(10);
console.log(filterAbove10([5, 8, 13, 21])); // [13, 21]The factory creates different filters without repeating the same code.
Example 4 - a factory with partial application
javascript
function multiplyBy(factor) {
return function(number) {
return number * factor;
};
}
const double = multiplyBy(2);
const triple = multiplyBy(3);
console.log(double(5)); // 10
console.log(triple(5)); // 15Convenient for creating specialized functions "on the fly".
Why this is convenient
| Benefit | Description |
|---|---|
| Code reuse | The factory creates many similar functions without duplicating logic |
| Data encapsulation | Uses closures to keep internal state |
| Flexibility | You can create functions with dynamic behavior |
| Functional style | Combines well with the "map / filter / reduce" patterns |
Example 5 - a real-world case (logger)
javascript
function createLogger(prefix) {
return function(message) {
console.log(`[${prefix}] ${message}`);
};
}
const info = createLogger('INFO');
const error = createLogger('ERROR');
info('Server started'); // [INFO] Server started
error('Something went wrong'); // [ERROR] Something went wrongOne template -> different functions, different contexts.
Summary
| Property | Description |
|---|---|
| What it is | A function that returns another function |
| Main idea | "Generate" functions with a shared template |
| Often uses | Closures |
| Where it's used | Configurations, handlers, loggers, counters, factory patterns |
| Benefits | Fewer repeats, more flexibility and readability |
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.