Understanding Pure and Non-Pure Functions in JavaScript

iamvivekaler
3 min readJun 13, 2024

--

Introduction

In functional programming, pure functions are a foundational concept. They play a crucial role in writing predictable, maintainable, and testable code. This article will explain what pure functions are, how they differ from non-pure functions, and provide examples to illustrate these concepts.

What is a Pure Function?

A pure function is a function that:

  1. Always produces the same output for the same input.
  2. Has no side effects (i.e., it does not modify any external state or data).

Example of a Pure Function

Let’s start with a simple example of a pure function:

const makePureFunc = (a, b) => {
// A pure function that given the same input, will always produce the same output and does not have any side effects.
return a + b;
};
let a = 2;
let b = 3;
console.log("Pure function :: same input/output", makePureFunc(a, b)); // Output: 5
console.log("Pure function :: same input/output", makePureFunc(a, b)); // Output: 5
console.log("Pure function :: same input/output", makePureFunc(a, b)); // Output: 5

In this example, makePureFunc takes two arguments a and b, and returns their sum. This function is pure because:

  • It always returns the same result (5) for the same inputs (2 and 3).
  • It does not modify any external variables or state.

What is a Non-Pure Function?

A non-pure function, on the other hand, does not guarantee the same output for the same input and/or may have side effects. Here’s an example:

let counter = 0;
const makeNonPureFunc = () => {
counter++;
return counter;
};
console.log("Non-pure function :: different input/output", makeNonPureFunc()); // Output: 1
console.log("Non-pure function :: different input/output", makeNonPureFunc()); // Output: 2
console.log("Non-pure function :: different input/output", makeNonPureFunc()); // Output: 3

In this example, makeNonPureFunc increments the counter variable each time it is called and returns the new value of counter. This function is not pure because:

  • It does not return the same result for the same input (it has no input parameters, yet returns different results on each call).
  • It modifies the external counter variable, introducing side effects.

Why Pure Functions Matter

Pure functions are beneficial for several reasons:

  • Predictability: Given the same inputs, a pure function will always return the same result, making it easier to understand and predict the behavior of your code.
  • Testability: Pure functions are easier to test because they don’t depend on external state. You can test them in isolation with a given set of inputs and expect consistent outputs.
  • Debugging: Since pure functions don’t have side effects, there are fewer places for bugs to hide. This makes debugging simpler.

Conclusion

Understanding the distinction between pure and non-pure functions is essential for writing clean, maintainable, and reliable code. Pure functions enhance predictability, testability, and ease of debugging, making them a key concept in functional programming and software development in general.

By using pure functions where possible, you can write code that is easier to reason about and maintain, leading to better overall software quality.

This article provides a clear explanation of pure and non-pure functions with practical examples, making it easier to understand their significance and how to implement them in JavaScript.

--

--

iamvivekaler

Hi, I'm Vivek Singh, a ReactJS Developer. Passionate about building interactive web apps. Skilled in ReactJS and Web Development.