Voice of a Developer: JavaScript Pure And Impure Function - Part Thirteen

Introduction

 
JavaScript is a language of Web. This series of articles will talk about my observations learned during my decade of software development experience with JavaScript.
 
In the last article I covered anonymous function. In this article I will cover pure and impure function in detail. Before I cover this, we should understand some aspects of functional programming so we know what is mutable and immutable objects.
 
Before moving further let us look at the previous articles of the series:

Functional programming

 
It is a different style of programming language that uses a declarative approach rather than imperative. In declarative programming, you are using a more descriptive way to define what you want to do and not how you want to do some action. Example, LINQ in C#
 
bottles.Where(price<20).OrderBy(brand).Take(10);
 
It will find bottles where the price is less than 20, order by brand, and take the top 10.
 
Another aspect of declarative programming is that it avoids changing state and mutable data. In simple terms, once a variable is bound to a value, it cannot be changed.
 
Two more concepts
 

Mutable

 
Mutable means it can be changed. Different programming languages have different approaches to the implementation of mutability or immutability.
 
In Javascript, Objects are mutable as they are addressed by reference and not by value.
 
Example
  1. var y={a:1, b:2};    
  2. var x =y;   
The above will not duplicate y in x, but it’ll point x to y. Please refer to objects (part 2) for detail.
 

Immutable

 
Immutable means it cannot be changed. In Javascript strings are immutable. Example,
 
code
 
You cannot change the string value. If you want to modify a string it’ll always return a new string. Remember using trim, slice, toUpper functions
 
code
 

Pure function

 
These are the functions that always return the same value when given the same arguments. They take some parameters, return a value based on these, but don’t change parameter values. An example product is a function that will always give the same output depending upon the input.
  1. function product(a, b) {  
  2.     return a * b;  
  3. }  
  4. console.log(product(2, 3));  
  5. // always give 6 whenever you pass 2, 3 
Another nice property of pure function is that it doesn’t modify the states of variables out of its scope.
 
Example
  1. var x = 10;  
  2.   
  3. function pureFunction(a) {  
  4.     return a + 2;  
 
code
 
pureFunction doesn’t modify the variable outside of its scope, i.e., “x”. It a nutshell pure function doesn’t have side effects.
 

Impure function

 
An impure function may have side effects and may modify arguments that are passed to them. The return value will depend upon the arguments. There may be a case where for same arguments you’ll get different values:
 
Example
  1. var count = 0;  
  2.   
  3. function Hits() {  
  4.     count += 1;  
  5. }  
  6. Hits(); // will make count 1    
  7. Hits(); // will make count 2    
  8. Hits(); // will make count 3 
Here it's using an external variable count and also modifying it. If a function has side effects whether it updates any file or database it’ll also fall under the category of impure function.
  1. function impureFunction(a) {  
  2.     file('update.txt');  

Summary of differences

 
Pure function Impure function
No side effects like update or DB calls May have side effects
Don’t modify arguments which are passed to them May modify arguments passed to them,
Always return the same value Even if you call with the same arguments, you may get different values.
 
I hope you enjoyed reading the article. Please share your comments or feedback.