Skip to content
Latest: Build and Master React Components Without Frameworks

Pure Function vs Impure Function – Learn the Difference

An impure function contains one or more side effects, while a pure function does not. In other words, your function is pure if it has no external code. Otherwise, it is impure if it includes one or more side effects.

Example 1: Impure Function

const myNames = ["Oluwatobi", "Sofela"];
function updateMyName(newName) {
myNames.push(newName);
return myNames;
}

In the snippet above, updateMyName() is an impure function because it contains code (myNames) that mutates an external state—which gives updateMyName() some side effects.

Example 2: Pure Function

We can eliminate updateMyName()’s side effects by turning it into a pure function like so:

function updateMyName(newName) {
const myNames = ["Oluwatobi", "Sofela"];
myNames[myNames.length] = newName;
return myNames;
}

In the snippet above, notice that updateMyName() does not depend on any external code to accomplish its duties. This makes it a pure function.

Use pure functions in your applications wherever possible. Let’s discuss some of the advantages of doing so.

Advantages of Pure Functions

The following are some advantages of pure functions.

1. Pure functions are independent

Pure functions do not affect any external state, and they are also not affected by external code.

In other words, all external data a pure function uses gets received as parameters. They are not explicitly used internally.

Therefore, what you see within is what you get—no strings attached. You don’t need to look for external conditions (states) that might impact your pure function’s effective operation as all activities happen within.

2. Pure functions are easier to read

Pure functions are easier to read and debug than their impure alternatives.

Pure functions are readable because they are solely dependent on themselves—they neither affect nor are they impacted by external states.

Important Stuff to Know about Pure Functions

Here are three essential facts to remember when you use pure functions.

You can clone an external state into a pure function

Cloning an external state into a pure function does not make the function impure.

State duplication is simply a copy-and-paste operation that does not leave any strings attached between the source and its clone.

Here’s an example:

const myBio = ["Oluwatobi", "Sofela"];
function updateMyBio(newBio, array) {
const clonedBio = [...array];
clonedBio[clonedBio.length] = newBio;
return clonedBio;
}
console.log(updateMyBio("codesweetly.com", myBio));

Try it on StackBlitz

In the snippet above, updateMyBio() used the spread operator to duplicate myBio’s state into clonedBio. However, it is still a pure function because it is neither dependent on myBio nor does it modify any external code.

Instead, it is an exclusively deterministic function programmed to use the cloned version of its array parameter.

Avoid code mutations in pure functions

Technically, you can mutate variables defined locally within a pure function’s scope. However, it is best to avoid doing so.

For instance, consider the code below:

const compBio = ["code", "sweetly"];
function updateCompBio(newBio, array) {
const clonedBio = [...array];
clonedBio[clonedBio.length] = newBio;
return clonedBio;
}
console.log(updateCompBio(".com", compBio));

Try it on StackBlitz

In the snippet above, updateCompBio() is a pure function that uses clonedBio[clonedBio.length] = newBio to alter its local state.

Although such an operation does not make updateCompBio() impure, it is not the best practice.

The recommended way to write a pure function is to make it receive all its values as parameters like so:

const compBio = ["code", "sweetly"];
function updateCompBio(newBio, array) {
return [...array, newBio];
}
console.log(updateCompBio(".com", compBio));

Try it on StackBlitz

Notice how clean and portable our code now looks. This is an advantage of making your pure function receive all its values as parameters. By so doing, you will also find it easier to debug your code.

The same input will always return the same output

A vital trait of pure functions is that they will always return the same value with the same set of inputs, no matter how many times they are invoked.