Syntax.

void expr

expr

undefined

> 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 5

void

function void(expr) { // not possible: syntax error! return undefined; }

evaluatesand returns. Examples:Thus, if you could manually implementas a function, it would look as follows:

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:

Use case: void 0 as undefined Use case: bookmarklets Use case: links that execute JavaScript code

Use case: void 0 as undefined

void 0

void(0)

undefined

undefined

> function a(undefined) { return undefined; } > a("hello") ’hello’ > undefined = "foo"; > console.log(undefined); foo

void

> 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"; ......^

undefined

andare the same as, with one exception:can be shadowed or redefined:In contrast,cannot change its meaning (the arrow always pointsthe offending token):In case you were wondering: strict mode does not prevent the value offrom 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.

Use case: bookmarklets

undefined

void

javascript:3+4 displays 7 as the current document.

displays as the current document. javascript:void(3+4) does not change the current document. void “hides” the result of the expression 3+4 .

javascript:window.open("http://www.whitehouse.gov/") replaces the content of the current page.

replaces the content of the current page. javascript:void window.open("http://www.whitehouse.gov/") does not change the content of the current page.

submit.example.com

javascript:void window.open("http://submit.example.com/submit?"+encodeURIComponent(document.location.href))

Use case: links that execute JavaScript code

void

computeResult()

<a href="javascript:void computeResult()">Compute</a>