Syntax.
void exprevaluates expr and returns undefined. Examples:
> void 0 undefined > void(0) undefined > void 4+7 // same as (void 4)+7 NaN > void(4+7) undefined > var x; > x = 3; 3 > void(x = 5); undefined > x 5Thus, if you could manually implement void as a function, it would look as follows:
function void(expr) { // not possible: syntax error! return undefined; }
Operator precedence. The operator is associated closely with its operand. You should therefore put parentheses around its operand if it contains more than a single token. For example, void 4+7 binds as (void 4)+7.
Three use cases for void. The following subsections describe three use cases for the void operator:
> function a(undefined) { return undefined; } > a("hello") ’hello’ > undefined = "foo"; > console.log(undefined); fooIn contrast, void cannot change its meaning (the arrow always points behind the offending token):
> function b() { var void = function() {}; return void(0); } missing variable name function b() { var void = function() {}; return void(0); } .......................^ > void = "foo"; syntax error void = "foo"; ......^In case you were wondering: strict mode does not prevent the value of undefined from being changed.
Best practice. If you are paranoid about shadowing or globally changing undefined, use void 0. Otherwise, use the more descriptive undefined; not everyone reading your code might be familiar with the void operator.
javascript:void window.open("http://submit.example.com/submit?"+encodeURIComponent(document.location.href))
This bookmarklet does not change the current page and displays the
confirmation of a successful submission in a new window/tab.
<a href="javascript:void computeResult()">Compute</a>