I've seen some people using void
operator in their code. I have also seen this in href
attributes: javascript:void(0)
which doesn't seem any better than javascript:;
So, what is the justification of using the void
operator?
Explanation of its use in links:
This is the reason that bookmarklets often wrap the code inside void() or an anonymous function that doesn't return anything to stop the browser from trying to display the result of executing the bookmarklet. For example:
javascript:void(window.open("dom_spy.html"))
If you directly use code that returns something (a new window instance in this case), the browser will end up displaying that:
javascript:window.open("dom_spy.html");
In Firefox the above will display:
[object Window]