What is the Pure Function
Function
function has special rules:
- It must work for every possible input value
- And it has only one relationship for each input value
Although each input will only have one output, but for different inputs may have the same output.
Pure function
Given all these, pure functions have a big set of advantages. They are easier to read and understand, as they do one thing. There is no need to look outside the function code and check for variables. There is no need to think how changing the value of the variable will affect other functions. No mutations in other functions will affect the result of the pure function for a specific input.
Pure functions are easier to test, as all dependencies are in the function definition and they do one thing.
For Example:
1 | var arr = [1, 2, 3] |
Another Example:
1 | // Impure |