What is the point of void operator in JavaScript?
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]
The undefined
value was not directly accessible in JavaScript until ES1.3.
An operator void <expression>
was therefore included to permit access to this value.
It is sometimes useful, particularly when working with the Web API (e.g. event handlers), to ensure that the result of an expression is consistently undefined
.
When the undefined
property was added to the global object in ES1.3 the utility of void
became non-obvious.
Hence your question.