Ahmad Usman
Ahmad Usman

Ahmad Usman

What's the difference between Pure and Impure functions?

Ahmad Usman's photo
Ahmad Usman
·Nov 3, 2020·

1 min read

Impure functions: They may change the passed in arguments and their return value doesn't solely depend on the arguments i.e functions that return values that depends on random calls. This they may generate different return value on different function calls with the same passed in arguments.

const increment = (num) => {
  num += 1; // Mutates passed in argument
  return num * Math.random(); // Return doesn't depend solely on arguments
}

Pure functions: They are the opposite of Impure functions💁‍♂️. They don't mutate their arguments, their return value depends solely on the passed in arguments this they always return the same value for different calls with the same arguments.

const multiply = (num1, num2) => {
  return num1 * num2;
}
 
Share this