Javascript Pure Functions: A Guide for Developers.

Mariano De Simone
6 min readMar 3, 2023

An essential topic any developer should learn

As a developer, you may have heard of the term “pure functions” in the context of programming languages. In JavaScript, pure functions do not cause any side effects and always return the same output given the same input. This article will dive deeper into pure functions, their helpfulness, and how to write them in JavaScript.

Snow
Pure as Snow — Photo by Jill Heyer on Unsplash

What is a Pure Function?

A pure function, as mentioned before, is a function that does not modify any external state or variable and does not rely on any external state or variable. Instead, it operates only on its inputs and returns a new value based on those inputs. In other words, the output of a pure function is determined solely by its inputs.

Here’s an example of a pure function:

function add(x, y) {
return x + y;
}

This function takes two arguments, x and y, and returns their sum. It does not modify or rely on any external state or variable, making it a pure function.

Here’s an example of an impure function:

let z = 0;

function add(x, y) {
z = x + y;
return z;
}

This function modifies the external variable z and returns its value. It also relies on the value…

--

--

Mariano De Simone

Javascript Developer, Digital Marketing Specialist, amateur runner and history & philosophy books fan. Love to read when I'm not coding or writing.