Open In App

Understanding the Difference between Pure and Impure Functions in JavaScript

Last Updated : 22 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see the concepts of pure and impure functions in JavaScript, along with understanding their differences, & basic implementation for a better understanding of the concepts.

Pure Functions: This function always returns the same output as given the same input parameters. Pure functions only depend on their input parameters and don’t affect the state of the application or other parts of the code.

The Side effect/leaking does happen when the function tries to utilize any external code inside the function, which in turn, impacts the ability of the function to perform that specific task for which the function is built.

 

Characteristics:

  • Functions do not modify any state or have any side effects.
  • Functions only depend on their input parameters.
  • These are predictable and have a deterministic behavior and are easier to test, debug, and maintain. 

Advantages:

  • Pure functions are easier to test because they have predictable behavior and do not modify any state or have side effects.
  • These can be composed together to create complex behaviors, and the composition is also pure.
  • Pure functions are used to memoize performance improvements since the same input will always return the same output and is less likely to introduce bugs or unexpected behavior in the code.

Disadvantages:

  • Pure functions can be more difficult to write when the problem requires state changes or side effects.
  • They require additional overhead to pass all necessary data as input parameters.

Example 1: This example describes the Pure function, Here, geek() is a pure function as it always returns the same output given the same input parameter. It does not modify any state or leave effects on the code. When the function geek is called it passes the output through the inside function and returns the value. 

Javascript




function geek(value) {
    return value+100;
}
  
console.log(geek(34)); 
console.log(geek(4));  
console.log(geek(12));


Output

134
104
112

For instance, geek(34) will return the output as 100+34 = 134. We can clearly see that It’s doesn’t modify the entire code.

Example 2: In this example, the capitalize() function takes a string as an input parameter and returns a new string with all characters in uppercase. Since it does not modify any state or have side effects, it is a pure function.

Javascript




function capitalize(str) {
  return str.toUpperCase();
}
  
console.log(capitalize('geeks')); // Output: GEEKS
console.log(capitalize('world')); // Output: WORLD


Output

GEEKS
WORLD

Impure Functions: Impure functions are functions that can modify the state of the application or have side effects. In other words, impure functions can have unpredictable behavior and do affect other parts of the application.

Characteristics:

  • Functions can modify the state of the application or have side effects.
  • Functions depend on other parts of the code.
  • These are unpredictable and can have a non-deterministic behavior and are harder to test, and maintain.

Advantages:

  • They can have side effects or modify the state of the application, which is necessary for many tasks such as I/O operations or DOM manipulation.
  • Impure functions are easier to write and may require fewer input parameters.
  • Functions are more flexible and adaptable to different use cases.

Disadvantages:

  • Impure functions are more difficult to reason about and test because their behavior can be unpredictable and depend on other parts of the code.
  • Functions can introduce bugs or unexpected behavior in the code.
  • They can be more difficult to compose together to create complex behaviors.

Example 3: In this example, the incrementflag() is an impure function because it modifies the flag variable which is outside the scope. The first time incrementflag() called flag value increased to 1 and print it in the console. But when the second time called, the increment() function increased the previous value of flag and 1, and the updated value is 2. So this impure function increment modifies the flag along with the state.

Javascript




let flag = 0;
function incrementflag() {
    flag++;
}
incrementflag();
console.log(flag); 
  
incrementflag();
console.log(flag);


Output

1
2

Example 4: In this example, the getRandomNumber() function generates a random number between 0 and 9 each time it is called, which is a side effect. Therefore, it is an impure function. Also, note that the function can have different outputs even with the same input (none in this case).

Javascript




function getRandomNumber() {
  return Math.floor(Math.random() * 10);
}
  
console.log(getRandomNumber());
console.log(getRandomNumber());


Output

7
4

Differences between Pure and Impure Functions:

Features

Pure Functions

Impure Functions

State Modification Does not modify the state of the application It can modify the state of the application
Dependency Only depends on its input parameters It  can be dependent on other parts of the code
Testing Easier to test harder to test
Maintenance It is easier to maintain It is harder to maintain

In summary, pure functions have several advantages over impure functions such as being easier to test, debug, and maintain. Pure functions only depend on their input parameters and do not modify the state of the application or have side effects. Impure functions, on the other hand, can have unpredictable behavior and can modify the state of the application or have side effects.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads