Building a Simple Calculator with the Factory Function Pattern
A small calculator is a perfect way to understand two core JavaScript ideas at once — the factory function pattern and closures. Here's how a single function can create an object whose methods remember a value long after the function has run.
Muhammad Ali

Two of the most useful ideas in JavaScript — the factory function pattern and closures — are easiest to understand with a small, concrete example. So let's build a tiny calculator and watch both concepts work together.
The code
function Code(num1) {
return {
sum(num2) {
return num1 + num2;
},
subtract(num2) {
return num1 - num2;
},
multiply(num2) {
return num1 * num2;
},
divide(num2) {
return num1 / num2;
},
};
}
const result = Code(5);
console.log("Sum:", result.sum(5)); // 10
console.log("Subtract:", result.subtract(5)); // 0
console.log("Multiply:", result.multiply(5)); // 25
console.log("Divide:", result.divide(3)); // 1.66...
What's happening, step by step
Code(5)is called, sonum1is5.- The function returns an object with four methods:
sum(),subtract(),multiply(), anddivide(). - That returned object is stored in
result. - We call the methods and pass a second value (
num2) to run each operation against the original5.
The factory function
A factory function is a function that creates and returns an object with properties and methods. Every time you call it, you get back a fresh object.
Here, Code is the factory. Call it with a starting number and it hands you a ready-made calculator object built around that number. Want a calculator that starts from 10 instead? Just call Code(10) — you get a brand-new, independent object.
The closure — the interesting part
Notice that every method still uses num1, even though Code() has already finished running by the time we call result.sum(5).
That works because of a closure. When the inner methods were created, they captured the num1 from their surrounding scope — and they hold onto it. So each method "remembers" the value it was built with, long after the outer function returned.
This also gives us something nice for free: num1 is private. There's no way to reach in from outside and change it — it only lives inside the closure, accessible only to the methods that need it. That's encapsulation, with no extra syntax.
Concepts in play
- Factory Function — one function that produces objects on demand
- Closure — methods that remember
num1after the function has run - Lexical Scope — the inner methods can see the outer function's variables
- Encapsulation —
num1stays private inside the closure
Why this matters
This calculator is tiny, but the pattern scales. Any time you want to create objects that share behavior but each carry their own private state — think of a counter, an API client with its own base URL, or a set of handlers built around some config — this exact combination of a factory function and closures is the tool for the job.
Start small, understand it here, and you'll spot the pattern everywhere.
Written by
Muhammad Ali
