Skip to main content

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

javascript
function sum(a, b) { return a + b; } console.log(sum(2, 3)); // 5 console.log(sum(2, 3)); // 5 - always the same

Each call is fully independent - the result depends only on the input data.


Example of a function with state

javascript
let counter = 0; function increment() { counter++; return counter; } increment(); // 1 increment(); // 2 - the result depends on the previous call

Here 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

TraitStateless function
Stores data between callsNo
Changes external stateNo
Depends only on argumentsYes
Always returns the same result for the same inputYes
ExampleMath.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 ready
Premium

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