Stateless function
In short: A stateless function is a function that does not store or change internal data between calls. It always behaves the same way for the same set of input arguments.
Detailed explanation
A "stateless" function remembers nothing about past calls and does not depend on external changes. Every call is an isolated computation, fully determined by its input parameters.
That is, such a function:
- has no internal memory (no counters, caches, flags);
- does not use global variables;
- does not change anything outside itself (the DOM, files, network requests).
Example of a stateless function
function sum(a, b) {
return a + b;
}
console.log(sum(2, 3)); // 5
console.log(sum(2, 3)); // 5 - always the sameEach call is fully independent - the result depends only on the input data.
Example of a function with state
let counter = 0;
function increment() {
counter++;
return counter;
}
increment(); // 1
increment(); // 2 - the result depends on the previous callHere the function "remembers" state (counter),
so it is stateful.
Where stateless functions matter
- In functional programming - the foundation of pure computations.
- In React (for example, "stateless components") - components that simply render data without their own state.
- In testable code - predictable results without dependence on the environment.
SUMMARY
| Trait | Stateless function |
|---|---|
| Stores data between calls | No |
| Changes external state | No |
| Depends only on arguments | Yes |
| Always returns the same result for the same input | Yes |
| Example | Math.max(a, b) |
In one phrase:
A stateless function is a function that has no "memory" of past calls, and its result always depends only on the input data, not on context.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.