60

Should I use void 0 or undefined in JavaScript to unassign a value, for example:

event.returnValue = void 0;

or

event.returnValue = undefined;
Davide Cannizzo
  • 2,826
  • 1
  • 29
  • 31
avo
  • 10,101
  • 13
  • 53
  • 81
  • 1
    There is no difference. `void 0` always returned undefined. –  Oct 14 '13 at 20:50
  • `event.returnValue` should be either `true` or false`, what is the case ? – The Alpha Oct 14 '13 at 20:50
  • look out chicken little!!! undefined is being defined all the time, judging from the answers below. really? – dandavis Oct 14 '13 at 20:53
  • @RecoveringSince2003, `returnValue` may contain a string for `onbeforeunload`, for example. – avo Oct 14 '13 at 20:54
  • You should use `void "do I understand why I'm doing this?"` – user2736012 Oct 14 '13 at 20:54
  • 1
    @user2736012, do you? – avo Oct 14 '13 at 20:56
  • I understand enough to not redefined the value of `undefined`. That's all one needs. – user2736012 Oct 14 '13 at 20:57
  • `event.returnValue` is alternative of `event.preventDefault()` which is for `IE-8` and lower to cancel the default behavior using `false`, but i didn't get what did you mean. – The Alpha Oct 14 '13 at 20:58
  • @user2736012, did I say I'm redefining `undefined`? My JavaScript module is self-contained and should be able to run in any context. – avo Oct 14 '13 at 20:58
  • @avo: Then why would you use `void 0`? – user2736012 Oct 14 '13 at 20:59
  • 2
    If your module is self-contained then you can take steps to ensure that inside the module `undefined` actually has the value `undefined` even when running in older browsers that allow the `undefined` global to be set to other values. @user2736012 - The point is that (in older browsers) some other included library may potentially have changed the value of `undefined` as a global, so it isn't completely safe to assume that `undefined` will always actually have the value `undefined`. – nnnnnn Oct 14 '13 at 21:01
  • @nnnnnn: IMO, if a developer loads a library that does this, that's the same as the developer doing it. Either way, a scoped `undefined` is a better solution. – user2736012 Oct 14 '13 at 21:06
  • To the down-voter: thank you for explaining why. – avo Oct 14 '13 at 21:06
  • @user2736012, I disagree that I should be trying to make sure `undefined` is correct in my scope (and fix it otherwise), if I don't use it at all. I'd stick with `void 0`. – avo Oct 14 '13 at 21:09
  • @user2736012, if you found it's a duplicate, feel free to use Close link. But thanks for explaining the reason to down-vote, usually people just don't. – avo Oct 14 '13 at 21:12
  • @user2736012 - The point is that you might be writing a library that is used by other people who don't take care about these things, so you need to ensure your own code copes. If you don't supply code to other people then you don't have to worry. – nnnnnn Oct 14 '13 at 21:15
  • @nnnnnn: Yes, I agree. That's why I said a scoped `undefined` is a good solution. I think anyone writing a public library will have enough sense to encapsulate their variables, and make a safe `undefined`. Though I wouldn't suggest trying to solve all "environment corruption" problems. At some point the end users just need to be responsible for their own bad code. – user2736012 Oct 14 '13 at 22:25
  • There is a difference -- `void 0` takes less bytes. If the goal is to minify the code, then `void 0` is better than `undefined`. But, this should be the job of whatever minifier you use to minify your code. – rpivovar Dec 01 '19 at 16:03
  • What about ```[][0]```? Is it safe Enough? – gkucmierz Jun 30 '20 at 12:32

3 Answers3

83

If you are using a modern browser, (which supports JavaScript 1.8.5) using undefined and void 0 would most likely yield the same result (since undefined is made not writable), except that the void can accept an expression as parameter and evaluate it.

In older browsers, (which do not support JavaScript 1.8.5) it is better to use void 0. Look at this example:

console.log(undefined);
var undefined = 1;
console.log(undefined);

It will print

1

undefined is actually a global property - it's not a keyword. So, undefined can be changed, where as void is an operator, which cannot be overridden in JavaScript and always returns the value undefined. Just check this answer which I gave earlier today for a similar question, Why does void in Javascript require an argument?.

Conclusion:

So, if you are concerned about compatibility, it is better to go with void 0.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • 2
    While it is a variable, it was given `[[Writable]]: false` [in ES5](http://es5.github.io/#x15.1.1.3). So, the example only *works* in ES3-based engines, like IE 8 and older. – Jonathan Lonowski Oct 14 '13 at 20:54
  • 1
    @JonathanLonowski depends on [the scope...](http://jsfiddle.net/antisanity/gFtJW/) – canon Oct 14 '13 at 20:55
  • @JonathanLonowski Included reference to the fact that `undefined` is made not writable. Thanks for pointing it out. Please check. – thefourtheye Oct 14 '13 at 20:59
  • 1
    Note that plenty of people _are_ still using IE8 (for example it's forced upon me by my employer), so better safe than sorry. – nnnnnn Oct 14 '13 at 21:07
  • 1
    I don't see the compatibility argument. While it may be overwritable in older environments, no one *does* overwrite it. I think `undefined` is better because a) it's more clear and b) you can easily make it a minifyable variable. – Bergi Oct 14 '13 at 22:48
  • @thefourtheye, You might want to migrate/post this answer to [the original thread](http://stackoverflow.com/q/5716976/632951) that has more views. – Pacerier Jul 25 '15 at 18:39
  • `undefined` is still writable in modern browsers in any scope other than the global scope. No one _should_ write to it, but if you're writing some sort of compiler/minifier, it sounds like it would be safer to use `void 0`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined#description – mofojed Aug 22 '23 at 14:14
14

"void 0" is safer. "undefined" is a value, like any other. It's possible to overwrite that value with another:

undefined = 3;

That would change the meaning of your event.returnValue had you used undefined. "void" is a keyword, though, and its meaning can't be changed. "void 0" will always give the value undefined.

Zach
  • 7,730
  • 3
  • 21
  • 26
  • 4
    Not in any modern browser it's not. – Colin DeClue Oct 14 '13 at 20:51
  • 1
    @ColinDeClue depends on [the scope...](http://jsfiddle.net/antisanity/gFtJW/) – canon Oct 14 '13 at 20:53
  • @canon: So then if you write properly scoped code, it's impossible for someone to overwrite `undefined` for you? – Colin DeClue Oct 14 '13 at 20:57
  • @ColinDeClue - At work I use IE8, because I don't have control over the desktop environment. My point being that we have to code pages on the assumption that they might run in older browsers. (Not that it isn't easy to deal with `undefined`.) – nnnnnn Oct 14 '13 at 21:08
  • @nnnnnn: It depends on your requirements, really. Sometimes, yes, you need to code with the assumption it needs to work on IE8. Other times that would increase the cost dramatically, and potentially decreased the feature set. – Colin DeClue Oct 14 '13 at 21:10
  • 1
    "Safer"? Whoever overwrites it on his own page is silly, and it's his own fault. If someone else can overwrite it on your page, you've got a more serious problem. – Bergi Oct 14 '13 at 22:42
  • @Bergi, some of us write libraries that are used on other people's pages, not just our own. Writing a library that can work on anyone's page (regardless of browser version or "silly" faults) requires techniques like this one. – Zach Oct 14 '13 at 23:53
  • @Zach: Every library does fail on a page whose ES environment is compromised enough. Still, to "fix" this particular fail you should not use `void`, but an IEFE: `(function(undefined){ /* use 'undefined' here */ })(void 0);` – Bergi Oct 15 '13 at 12:13
  • 2
    @Bergi I noticed you are still using `void 0` in your example of how to fix it. :) Of course you don't have to -- once you're using a function, you could do `(function(undefined) { /* use 'undefined' here */ })();` (not passing a parameter gives the value undefined), or this: `(function() { var undefined = (function(){})(); /* use 'undefined' here */ })();` The point being, there are lots of ways to get the value undefined. `void 0` is strictly safer than just using `undefined` without a function scope. – Zach Oct 15 '13 at 16:00
1

The void operator is often used merely to obtain the undefined primitive value, usually using “void(0)” (which is equivalent to “void 0”). In these cases, the global variable undefined can be used instead (assuming it has not been assigned to a non-default value).

https://stackoverflow.com/a/1291950/2876804

Just use undefined, since they will both evaluate to it.

Community
  • 1
  • 1
shade4159
  • 178
  • 2
  • 8
  • -1: I would agree to just use undefined for normal code, but it's not always true that they'll both evaluate to it. This answer also doesn't discuss why one would use one alternative over the other, which does have practical reasoning. – Waleed Khan Oct 14 '13 at 20:51
  • @Khan This and another user basically copied/pasted from other questions. –  Oct 14 '13 at 20:52