Suggest an editImprove this articleRefine the answer for “Pure function”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**A pure function is a function that always returns the same result for the same input and has no side effects, meaning it does not change external variables, the DOM, files, the network and so on.** So it has exactly two key properties: determinism (the same input always gives the same output) and the absence of side effects (the function changes nothing outside itself). What makes a function impure is touching external state or using `Math.random()`, `Date.now()`, the DOM, the network, files or `console.log()`. Pure functions are easy to test and to cache (memoization), they are safe for parallel execution and they make code more predictable. ```javascript function add(a, b) { // pure: deterministic, no side effects return a + b; } ``` **Key point:** a pure function does not affect the outside world and always behaves the same way for the same input.Shown above the full answer for quick recall.Answer (EN)Image**A pure function is a function that always returns the same result for the same input and has no side effects.** That is, it does not change external variables, the DOM, files, the network or anything at all outside itself. ## Theory ### TL;DR - A pure function is deterministic: the same input always gives the same output. - A pure function has no side effects: it changes nothing outside itself. - It does not depend on external state, which makes it predictable and testable. - What makes a function impure: changing external variables, the DOM, files, the network, `console.log()`. - `Math.random()` and `Date.now()` are impure too, because the result differs each time. - Pure functions are easy to cache (memoization) and safe to run in parallel. - This is a fundamental concept of functional programming. ### Quick example ```javascript // Pure function function add(a, b) { return a + b; } // Impure function let counter = 0; function increase() { counter++; // side effect: changes an external variable return counter; } ``` ### The two key properties A pure function is a fundamental concept of functional programming. It is **predictable**, **testable** and **independent of external state**. 1. **Determinism.** The same input always produces the same output. 2. **No side effects.** The function changes nothing beyond its own boundaries. Both properties are required. A function that returns a stable result but writes something to a log or a global object along the way is already impure. ### An example of a pure function ```javascript function add(a, b) { return a + b; } ``` It always returns the same result and changes nothing in the outside world. You can call it as many times as you like in any order and the behaviour of the program will not change because of it. ### An example of an impure function ```javascript let counter = 0; function increase() { counter++; // side effect: changes an external variable return counter; } ``` This function is **impure**, because its result depends on external state (`counter`) and it modifies that state. Two identical calls to `increase()` return different values, which already breaks determinism. ### Other examples of impure functions - reading from or writing to a file or a database; - changing the DOM; - calling `alert()` or `console.log()`; - using random numbers (`Math.random()`); - using the current time (`Date.now()`). A special case is mutating the arguments: if a function changes an object or array passed into it, it is impure too, even when it never touches a global variable. ```javascript // Impure: mutates the input array function addItemImpure(list, item) { list.push(item); return list; } // Pure: returns a new copy function addItem(list, item) { return [...list, item]; } ``` ### What pure functions are for - Easy to test, because the result depends only on the arguments. - Easy to cache (memoization). - Safe for parallel execution. - They increase the predictability and readability of code. | Property | Pure function | | --- | --- | | Returns the same result | Yes | | Depends on external variables | No | | Changes external state | No | | Example | `Math.max(1, 5)` | | Opposite | A "dirty" function with side effects | > A pure function is a function that **does not affect the outside world** and **always behaves the same way for the same input**. ### Common mistakes - **Calling a function pure when it mutates an argument.** `list.push(item)` changes the caller's object, so that is a side effect. - **Forgetting about `console.log()`.** Formally it is output to the outside, so strictly speaking a function that logs is already impure. - **Confusing purity with not having a `return`.** Purity is about determinism and side effects, not about whether there is a result. - **Using `Date.now()` or `Math.random()` inside.** Pass such values in as an argument, and the function becomes pure and testable again. - **Trying to make the whole program pure.** Side effects are what make it do anything at all; you push them to the edges of the application and keep the core logic pure.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.