I think this is a pure function as it abide by the rules to be a pure function.
- It always return the same output.
- It has no side effects.
But in a medium blog, they said that this is a impure function.
function addNumbers(){
let num1 = 100;
let num2 = 1;
return num1 + num2;
}
addNumbers();
3
Answers
Yes, it’s a pure function because it follows two key rules :
No side effects
Same input, same output
The function you provided is an example of a pure nullary function, read this answer for more details.
According to this article:
Since the function does not take any inputs, it will always return the exact same ouput, and it does not modify any external states, hence the function is pure.
Your function is not pure because it depends on hardcoded values instead of external inputs.
In your case your are breaking the following pure function rule.
Updating to following will make your function pure