EDIT: To clarify: My question isn't concerned with how void is used but whether void can be really useful. I'm asking this because the way people seem to use it is very offputting...
The MDN Reference for the void operator makes an example of the operator in use
<a href="javascript:void(0);">Click here to do nothing</a>
<a href="javascript:void(document.body.style.backgroundColor='green');">Click here for green background</a>
Not exactly the sort of way we handle click events nowadays.
Is there a place for void in this world? Can someone show me a good example of using void?
Use case #1, void(0): If you want a reference to the real undefined (and not just the variable, because it can be overwritten) you don't need void. You can get it like this instead: (function(){}()).
Use case #2, void(exp): If you want to execute code and then return undefined, you can of course do it by wrapping your code in a function: (function(){ exp; return undefined; }()).
So, no, we don't need void. It does nothing unique. It is shorter than the above solutions though, so if you prefer short and obscure code you could use it (but please don't).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With