
Pure Functions in JavaScript

When it comes to functional programming (in JavaScript or otherwise), the concept of a 'pure function' is a bit of a cornerstone: something all developers should understand (and it's not difficult to do).
What is a Pure Function?
Fundamentally, a pure function is one which meets two specific characteristics:
Deterministic Output
Given the same set of inputs, the function will return the same output every time without exception. A pure function's output is entirely determined by its input values, making its behaviour predictable and reliable.
No Side Effects
Pure functions operate in isolation, within their own scope. They do not affect or rely on external states or data outside of their own scope. They do not alter global variables, manipulate the DOM, or execute any other operation that affects the state outside of its own scope.
Examples of Pure and Impure Functions
To illustrate the 'pure versus impure' concept, let's take a look at a couple of examples.
A Pure Function
const add = (a: number, b: number) => a + b;A very basic example, but it illustrates the concept nicely. This add function is pure because for any specific inputs (a and b), it will always return the same output. It also does not interact with nor alter any state outside of its scope.
An Impure Function
let value = 2;const addImpure = (a: number) => { value += a; return value;};Again, it's a very basic example, but here the addImpure function is impure because its output depends on the external variable value. It also modifies this external state, outside of its scope.
Benefits of Pure Functions
There are a number of advantages to using pure functions in your JavaScript development (where the actual functionality allows).
Predictability
Since we know that the output is only determined by the input, pure functions are incredibly predictable. This makes debugging and enhancing the functionality very straightforward.
Reusability
Because pure functions are deterministic and absent of side effects, it means that a pure function can be easily reused across different parts of an application.
Testability
Related to number one, because we know that the output of a function will always be the same given the same inputs, adding test coverage to a pure function is very simple, particularly without worrying about external dependencies or mocked states.
Optimisations
When it comes to optimising your application, pure functions lend themselves very nicely to compiler optimisations like memoization (which is where the results of function calls are cached based on input parameters). This can drastically improve performance for recurring calls with the same arguments.
Concurrency
With no side effects and without dependencies on external state, pure functions are inherently thread‑safe, meaning we can avoid common issues around race conditions and the like.
Wrapping up
If nothing else, then all you need to take away from this is that pure functions are predictable, reusable, will always return the same output from the same inputs, and do not have side effects. Easy!
Categories:
Related Articles

Understanding the Composition API in Vue 3. 
Understanding and Solving Regular Expression Matching. Understanding and Solving Regular Expression Matching

Prepending PHP to a Page in Gatsby. Prepending PHP to a Page in Gatsby

Reduce() in JavaScript. reduce()in JavaScript
10 Essential SEO Tips for Front‑End Developers. 10 Essential SEO Tips for Front‑End Developers

Type Coercion in JavaScript: Implicit vs. Explicit Conversion. Type Coercion in JavaScript: Implicit vs. Explicit Conversion

JavaScript String Manipulation: substring() vs. substr(). JavaScript String Manipulation:
substring()vs.substr()
The Role of Dependency Injection in Angular. The Role of Dependency Injection in Angular

LeetCode: Converting Integers to Roman Numerals. LeetCode: Converting Integers to Roman Numerals

Understanding JavaScript's sort() Method. Understanding JavaScript's
sort()Method
Adding Static Files to a Gatsby Site. Adding Static Files to a Gatsby Site

Building a Headless CMS‑Powered Site with Next.js. Building a Headless CMS‑Powered Site with Next.js