1

The javascript void statement evaluates an expression, and returns the undefined value. This is useful because the global variable undefined can be redefined, but why does it evaluate an en expression? Most people just use 0, as in

void 0;
void(0);

Looking at Raghu's answer, the documentation says

This operator allows inserting expressions that produce side effects into places where an expression that evaluates to undefined is desired.

But can you give me an example where you can't just put void(0) in the location where the value ùndefined` is desired, and evaluate the expression with side effects on another line?

bigblind
  • 12,539
  • 14
  • 68
  • 123
  • 1
    Why would anyone ever do this? It's terribly opaque. – Robert Harvey Sep 23 '13 at 23:18
  • possible duplicate of [What does "javascript:void(0)" mean?](http://stackoverflow.com/questions/1291942/what-does-javascriptvoid0-mean) – Bucket Sep 23 '13 at 23:20
  • Related, but I'm not sure it's a duplicate. – Robert Harvey Sep 23 '13 at 23:21
  • @Robert Harvey It is, but if you're writing a third party library that runs on a page you don't control, it's useful to have a function/statement that always returns the value `undefined`, because the global variable `undefined`, which is initially set to the undefined value, can be changed by the code on the page. It's perfectly valid to say `undefined=3.141592`, in which case things like `ìf(x===undefined){...}` won't work, because `undefined` now equals 3.141592. It's weird to redefine undefined, but if you don't want you want your library to work in *any* page, you have to use void. – bigblind Sep 23 '13 at 23:26
  • Exceptionally useful for bookmarklets. Wrap your code with it (properly, of course) and it won't navigate the page anywhere. Then again, I just wrap my code in an IIFE, which is easier XD Okay, maybe not to "exceptionally useful"! – Niet the Dark Absol Sep 23 '13 at 23:40
  • Also @DesertIvy, it's not a dupe. The question you point to focusses on the use of void(0) as the href value of links. – bigblind Sep 23 '13 at 23:40

2 Answers2

2

The main use of the void operator is (or was, as today you probably rather use event-handlers) to prevent any navigation when clicking links with a javascript: URL. No navigation occurs when this javascript function returns undefined. The void operator is normally the shortest way to achieve that.

Example

<a href="javascript: void console.log('click') ">log click to console</a>

or, more general

<a href="javascript: void doSomething() ">do something</a>

as seen in this fiddle

Thorben Croisé
  • 12,407
  • 8
  • 39
  • 50
1

The standard documentation should help in understanding on why it evaluates as an expression and where and why most people use it

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void

Raghu
  • 2,543
  • 1
  • 19
  • 24
  • so it makes sure that the expression can be evaluated, making its side effects occur, without returning anything. But can you give me an example of a case where you couldn't just put the expression on a line without an assignment, so that its return value isn't saved anywhere? – bigblind Sep 23 '13 at 23:32
  • 1
    function getValue(){ var a,b,c; a = void ( b = 5, c = 7 ); document.write('a = ' + a + ' b = ' + b +' c = ' + c ); } – Raghu Sep 23 '13 at 23:58