DEV Community

Cover image for What is a Pure Function?
Helder Burato Berto
Helder Burato Berto

Posted on • Updated on • Originally published at

What is a Pure Function?

Originally posted on [helderburato](

Whenever you are told about pure functions, keep the following in mind.

Pure Functions are functions the given an input parameter will always return the same output without causing side effects.

When do Side Effects occur?

Side Effects occur when an executed function changes state within the application, which is known as impure functions.

Why Pure Functions?

I will mention some topics that make the use of function so interesting. See below:


Refactor the code wherever possible, pure functions give you the ease of change so you can observe improvements and apply them without affecting the rest of your application.


For the simple reason that pure functions have their input and output values determined, this will greatly facilitate the writing of their unit tests.

DRY (Don’t Repeat Yourself)

Reuse your functions!

Should I always use Pure Functions?

Not! It’s important to realize that pure functions while offering several benefits aren’t used throughout the project. After all, if all the functions of your project were pure functions, there would be no side effects where they are visible to the outside world. Be sure to use when necessary, create unit tests and no doubt when there are bugs, it will be easier for you to uncover and correct them.

Let’s practice!

Create two functions a pure function and another impure function. Check out:

Pure Function

## ES6 ##
const sum = (x, y) => x+y;

## ES5 ##
var sum = function (x, y) {
  return x + y;
Enter fullscreen mode Exit fullscreen mode

Impure Function

## ES6 ##
const x = 20;
const sum = (y) => x+y;

## ES5 ##
var x = 20;
var sum = function (y) {
  return x+y;
Enter fullscreen mode Exit fullscreen mode

Note that the variable x is being defined in the global state of the application, so the output of the sum function will always depend on the global state change and not on the input passed as a parameter, which makes the function dependent on external factors.


That’s it, folks, I hope this post helps you in developing nice functions, generating more productivity for you and your team.

Ah, if you have any question or suggestion, do in the comments of the post, I’m always attentive!

Top comments (0)