With this article, we will explore what pure/impure functions are and their differences in JavaScript. Let's dive in..
What is a funct...
For further actions, you may consider blocking this person and/or reporting abuse
Interestingly, due to the way JS works, it is possible that this function:
could cause side effects, and even return a different value each time it's called - even with the same inputs. It all depends upon what
aandbare. So, in a VERY strict sense it can be argued that it isn't pure. I'll explain further if you don't know what I'm getting at.Different values each time:
Side effects:
Well on the first example, the inputs are not the same in both executions, because
performance.now()returns a value, hence you are callingadd(1, 22278);the first time andadd(1, 49557);the second one. By that meansaddis still a pure function in the first example.I get your point on the other one and there are tones of nuances into this topic, in this specific use-case TS helps a lot:
So you end up with fine tuned control on how the given function is used. Of course you can still do dynamic stuff and trick everything in runtime but still you need to explicitly put effort into breaking it most of the time (specially if the types are well defined everywhere).
Best regards
Not at all, the inputs are identical each time. The second of which is an object, with an unchanged method. Nothing about the input to the
addfunction has changed on the second call. JS itself will call thevalueOffunction during coercion - after it has been passed in.We both know each other from the community and I'm pretty sure you understand that abstracting an impure function such
performance.now()doesn't avoid getting different values each time you call it (to a precision of 5 milliseconds in this case). I don't have time right now to discuss with troll statements to stress it further (even though you know I like it a lot)The usage of
valueOfin the example is pretty neat, though 😂You've misunderstood the code. I was just pointing out that the inputs to
addon both calls are the same. I'm actually curious if it would be possible to write a 'true' pure function in JS that would take into account all the coercion quirks 🤔valuable article
Thanks for your feedback bro
This is a great walkthrough of pure functions.
This might not apply to JS but in some languages, e.g. C#, it's possible to change the next line that gets run in the debugger. Pure functions are great because of their idempotence (i.e. the same inputs give the same outputs, as mentioned in the article), meaning that they can be run over and over if necessary during the debugging process to understand what's going on.
By they way, how did you create the cool animated graphics?
Thank you my friend for bringing up an interesting point of pure functions with debugging.. BTW I use canva.com to create animated graphics..
Interseting article
Thank you bro
Сongratulations 🥳! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up 👍
Thanks a ton man
One more benefit of pure functions - they can be memoized, b/c of their nature to produce the same result for the same arguments
Make sense. Thank you for sharing your thoughts
Interesting, thx!
Thank you boss