Skip to main content

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:

  • createGreeter is the factory;
  • sayHi and sayBye are 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()); // 11

Each 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)); // 15

Convenient for creating specialized functions "on the fly".


Why this is convenient

BenefitDescription
Code reuseThe factory creates many similar functions without duplicating logic
Data encapsulationUses closures to keep internal state
FlexibilityYou can create functions with dynamic behavior
Functional styleCombines 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 wrong

One template -> different functions, different contexts.


Summary

PropertyDescription
What it isA function that returns another function
Main idea"Generate" functions with a shared template
Often usesClosures
Where it's usedConfigurations, handlers, loggers, counters, factory patterns
BenefitsFewer repeats, more flexibility and readability

Short Answer

Interview ready
Premium

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