Content taken from this link
Characteristics of Pure Function:
The return value of the pure functions solely depends on its arguments Hence, if you call the pure functions with the same set of arguments, you will always get the same return values.
They do not have any side effects like network or database calls
- They do not modify the arguments which are passed to them
Characterisitcs of Impure functions
The return value of the impure functions does not solely depend on its arguments Hence, if you call the impure functions with the same set of arguments, you might get the different return values For example, Math.random(), Date.now()
They may have any side effects like network or database calls
They may modify the arguments which are passed to them
function impureFunc(value){
return Math.random() * value;
}
function pureFunc(value){
return value * value;
}
var impureOutput = [];
for(var i = 0; i < 5; i++){
impureOutput.push(impureFunc(5));
}
var pureOutput = [];
for(var i = 0; i < 5; i++){
pureOutput.push(pureFunc(5));
}
console.log("Impure result: " + impureOutput); // result is inconsistent however input is same.
console.log("Pure result: " + pureOutput); // result is consistent with same input