DEV Community

Discussion on: Functional Programming with JS

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Your 'adding' example of a pure function is kind of a pure function, but not for the reason you think it is. Your function does not have a return, so is implicitly returning undefined - so, yes, it always returns the same thing given the same inputs - but in this case, that 'same thing' is always undefined.

Another reason that it isn't really 'pure' is that it completely relies on outside state - i.e. console.log - which it has no control over - someone could easily redefine console.log to have different behaviour.

Also, pure functions should try to avoid side effects (modifying state outside of the function) - console.log is clearly a side effect

Collapse
 
trezeguit profile image
Mahmoud Hassan

Thanks a lot for your note, I will rewrite this example.