49

What exactly is the difference between undefined and void 0 ?

Which is preferred and why?

Pacerier
  • 76,400
  • 86
  • 326
  • 602
  • 1
    Most recent answer is here http://stackoverflow.com/a/19369078/1903116 , in a duplicate question. – thefourtheye Oct 15 '13 at 05:31
  • Related posts - [difference between “void 0 ” and “undefined”](https://stackoverflow.com/q/4806286/465053), [Why does void in Javascript require an argument?](https://stackoverflow.com/q/19367589/465053), [What is the point of void operator in JavaScript?](https://stackoverflow.com/q/666936/465053), & [What does “javascript:void(0)” mean?](https://stackoverflow.com/q/1291942/465053) – RBT Jul 14 '19 at 07:58

4 Answers4

65

The difference is that some browsers allow you to overwrite the value of undefined. However, void anything always returns real undefined.

undefined = 1;
console.log(!!undefined); //true
console.log(!!void 0); //false
user3840170
  • 11,878
  • 11
  • 37
duri
  • 13,813
  • 3
  • 41
  • 48
  • 9
    `void` is an operator and not a function so you dont have to use `void()`. – Teemoh Apr 06 '17 at 17:35
  • If browsers let you redefine `undefined`, shouldn't such browsers therefore also *use* the redefined `undefined` in these cases? What possible point could there be in redefining `undefined` otherwise? – devios1 May 30 '18 at 20:25
  • @devios1 It does. Anywhere `window.undefined` is referenced, it will have the new value which is what the code above shows. – Kevin Beal Jul 24 '18 at 23:51
  • @ManPersonson In that case it's not redefining it, it's just defining a similarly-named value in a different scope (window). – devios1 Aug 03 '18 at 18:50
  • 1
    `undefined` is a primitive value. There is no meaningful distinction there. It has exactly the same consequence for your program. – Kevin Beal Aug 05 '18 at 21:31
  • does it make to define `undefined`? Here we are using primitive `undefined` rather a variable – mukuljainx Aug 25 '19 at 18:09
  • which browser overwrite the value of undefined? – newpost Apr 13 '20 at 14:25
  • the answer not right anymore! ![](https://img2020.cnblogs.com/blog/740516/202009/740516-20200927130728713-127423232.png) – xgqfrms Sep 27 '20 at 05:07
27

undefined has normal variable semantics that not even strict mode can fix and requires run-time look-up. It can be shadowed like any other variable, and the default global variable undefined is not read-only in ES3.

void 0 is effectively a compile time bulletproof constant for undefined with no look-up requirements. It is like writing null or true, instead of looking up a variable value. It works out of the box without any safety arguments and is shorter to write. It is better in every way.

Esailija
  • 130,716
  • 22
  • 250
  • 308
  • 2
    Shorter just in uncompressed code; if you use an [IIFE](http://benalman.com/news/2010/11/immediately-invoked-function-expression/), you can minify `undefined` to something like `a`, while `void 0` is not minifiable :) – gustavohenke Oct 15 '13 at 04:20
  • 1
    @gustavohenke But what about gzip? If you minify you might get all different letters like `a` `b` `c` in different closures for `undefined`. Where as `void 0` is `void 0` anywhere. Anyways, this technique will surely confuse the JITs which already optimize `undefined` (FF, Chrome but not IE10) because it's a global constant and even static analysis can see if it's shadowed. With this, you turn it into a normal variable look-up and it's not a compile-time constant for sure. Of course that might not matter but that wasn't the argument :P – Esailija Oct 15 '13 at 04:23
  • 1
    @gustavohenke see http://pastebin.com/DDTp7E9c `void 0` and `undefined` are recognized as compile-time constants but saving it into a variable is totally different – Esailija Oct 15 '13 at 04:32
0

Use undefined. Its more commonly known than void(0).

Daniel A. White
  • 174,715
  • 42
  • 343
  • 413
  • 16
    Just because it's more commonly known doesn't mean it's better or can be used interchangeably. – Mike Mar 04 '15 at 09:26
-2

Parentheses here are optional, void 0, void(0) and void (0) are equivalent. The void is a unary operator with a right-to-left associativity, hence the value is placed at the right of it:

void <VALUE>.

For second question, you need to use undefined directly while avoiding unneeded operand evaluation to retrieve the same undefined value.

More info in the reference: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/void

KostasX
  • 2,364
  • 1
  • 12
  • 21
user422039
  • 1,371
  • 3
  • 12
  • 27
  • 25
    I dont think parentheses are optional because "JS is very loose at syntax", but because `void` is an operator not a function. – thefourtheye Oct 15 '13 at 05:30
  • 'you need to use `undefined` directly while avoiding unneeded operand evaluation to retrieve the same `undefined` value.' – what does that even mean? – user3840170 Oct 09 '19 at 14:36
  • **This is actually not true!** `undefined` is a global (on to of `window`) variable which holds by default the value of the result operator `void 0` and in some old browsers can be "re-writable". – Slavik Meltser Sep 22 '20 at 10:14