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?
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?
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
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.
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.
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.
void 0
is that the 0
is the de-facto standard for using void
to generate an undefined
value. –
Perpend undefined
is not a keyword, it's just a property of the global object. Second, in ECMAScript5, it is write protected, so the void 0
trick is only neccessary if someone uses undefined
as a local variable within a function. –
Coccidioidomycosis undefined
to be mutated. –
Genni if (typeof options.something === 'undefined')
which I previously remembered from SO being the safe pattern for checking for undefinedness? –
Hernia void x
always returns undefined. typeof x === "undefined"
checks if x
is undefined. –
Genni undefined
as an argument in an immediate function: (function(undefined){ ... }())
Which is specially useful for minification. –
Respective undefined
's value to be changed? –
Molliemollify undefined
when pasted into the console. I've seen odd things happen in the console, can't remember why or where I read about it though! Good to know. –
Molliemollify undefined
cannot be reassigned. The issue appears to be that undefined
is not a reserved keyword. Try your fiddle again without the var
prefix, making it an assignment rather than a declaration. –
Manzoni undefined
cannot be reassigned in current Chrome or Firefox versions. –
Manzoni undefined
is changing, but that a variable is shadowing it. –
Genni window.onload = function() { var undefined = 4; alert(undefined); }
. Attempting to shadow the global on Chrome, Firefox and Node.JS doesn't seem to work for me. –
Manzoni void undefined
would be slightly clearer than void 0
, no? –
Greenockite void 0
is a widely-known Javascript idiom whereas void undefined
is less common and might cause people to think a little bit more about it. –
Genni void 0
and not just void
? –
Dvorak void
requires an argument, so using it without anything after it would be a syntax error. –
Genni void
keyword takes an argument? It returns undefined
anyway, so why not just leave it as return void
and nothing more? –
Bayern (() => {'use strict';alert(undefined);let undefined = 2;}())
(because "undefined" refers to the local variable in the next line, not the global property). –
Valois typeof myVar === 'undefined'
has the advantage of not throwing a ReferenceError in console where as myVar === void 0
does, so can someone tell me why i should not keep using the old way for the "extra layer of safety" if they are otherwise functionally equivalent? –
Vaporetto void 0
returns undefined and can not be overwritten while undefined
can be overwritten.
var undefined = "HAHA";
void
is a reserved JavaScript keyword. It evaluates the expression and always returns undefined
.
undefined
, and also is guaranteed to return it (unlike the global overwriteable undefined
). –
Ailssa © 2022 - 2024 — McMap. All rights reserved.
void
is a special keyword with unique syntax. I had no idea thatvoid(0)
andvoid 0
were the same keyword until I found this question. – Facial