596

Reading through the Backbone.js source code, I saw this:

validObj[attr] = void 0;

What is void 0? What is the purpose of using it here?

1
  • 13
    The linked question about "void(0)" does not make it very clear that void is a special keyword with unique syntax. I had no idea that void(0) and void 0 were the same keyword until I found this question.
    – Coderer
    Jan 20, 2020 at 11:48

3 Answers 3

1182

What does void 0 mean?

void[MDN] is a prefix keyword that takes one argument and always returns undefined.

Examples

void 0
void (0)
void "hello"
void (new Date())
//all will return undefined

What's the point of that?

It seems pretty useless, doesn't it? If it always returns undefined, what's wrong with just using undefined itself?

In a perfect world we would be able to safely just use undefined: it's much simpler and easier to understand than void 0. But in case you've never noticed before, this isn't a perfect world, especially when it comes to Javascript.

The problem with using undefined was that undefined is not a reserved word (it is actually a property of the global object [wtfjs]). That is, undefined is a permissible variable name, so you could assign a new value to it at your own caprice.

alert(undefined); //alerts "undefined"
var undefined = "new value";
alert(undefined) // alerts "new value"

Note: This is no longer a problem in any environment that supports ECMAScript 5 or newer (i.e. in practice everywhere but IE 8), which defines the undefined property of the global object as read-only (so it is only possible to shadow the variable in your own local scope). However, this information is still useful for backwards-compatibility purposes.

alert(window.hasOwnProperty('undefined')); // alerts "true"
alert(window.undefined); // alerts "undefined"
alert(undefined === window.undefined); // alerts "true"
var undefined = "new value";
alert(undefined); // alerts "new value"
alert(undefined === window.undefined); // alerts "false"

void, on the other hand, cannot be overidden. void 0 will always return undefined. undefined, on the other hand, can be whatever Mr. Javascript decides he wants it to be.

Why void 0, specifically?

Why should we use void 0? What's so special about 0? Couldn't we just as easily use 1, or 42, or 1000000 or "Hello, world!"?

And the answer is, yes, we could, and it would work just as well. The only benefit of passing in 0 instead of some other argument is that 0 is short and idiomatic.

Why is this still relevant?

Although undefined can generally be trusted in modern JavaScript environments, there is one trivial advantage of void 0: it's shorter. The difference is not enough to worry about when writing code but it can add up enough over large code bases that most code minifiers replace undefined with void 0 to reduce the number of bytes sent to the browser.

26
  • 7
    but why does it need a parameter? any useful examples of voiding an expression? why void 0? and not void 1?
    – Anton N
    Dec 5, 2012 at 15:01
  • 2
    So this is basically the same as if (typeof options.something === 'undefined') which I previously remembered from SO being the safe pattern for checking for undefinedness?
    – Sam
    Feb 14, 2013 at 7:39
  • 4
    @Sam Not really, void x always returns undefined. typeof x === "undefined" checks if x is undefined. Feb 14, 2013 at 13:44
  • 5
    Another common pattern is to provide undefined as an argument in an immediate function: (function(undefined){ ... }()) Which is specially useful for minification.
    – AvL
    Jul 8, 2013 at 15:02
  • 2
    Interesting that current versions of Chrome and Firefox don't then! :)
    – rcambrj
    Aug 1, 2013 at 14:17
128

void 0 returns undefined and can not be overwritten while undefined can be overwritten.

var undefined = "HAHA";
5
  • 30
    ECMAScript 5 defines undefined as read-only
    – licancabur
    Apr 30, 2014 at 14:40
  • 2
    @licancabur: I believe that is in strict-mode code only, yes?
    – crowder
    May 9, 2014 at 18:09
  • 68
    Wow, I keep getting downvotes on this. When this was posted 3 years ago, browsers allowed you to set undefined to any value. May 30, 2014 at 12:50
  • 13
    If an answer becomes redundant over time why should it not be downvoted in favour of a more up-to-date answer?
    – ESR
    May 29, 2017 at 1:29
  • @epascarello, Although what you wrote there is correct (somehow), but I don't think it is answering the question (clear enough) that is asking what does that mean.
    – AaA
    Aug 1, 2017 at 7:25
42

void is a reserved JavaScript keyword. It evaluates the expression and always returns undefined.

1
  • 8
    It's probably shorter than undefined, and also is guaranteed to return it (unlike the global overwriteable undefined). Sep 17, 2011 at 4:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.