rwaldron / idiomatic.js

Principles of Writing Consistent, Idiomatic JavaScript
Other
24.84k stars 3.48k forks source link

void 0 #98

Closed yukulele closed 11 years ago

yukulele commented 11 years ago

a fast, short and reliable method to check undefined:

if(a === void 0){

}
pluma commented 11 years ago

I disagree with the reasoning: in modern JavaScript, undefined seems to be no longer overwriteable (though writing to it does not cause any exception). The expression void 0 may be shorter than undefined but that seems like a case of premature optimisation.

Historically, the overwrite-safe idiom was if (typeof a === 'undefined') {/* ... */}. The problem with void 0 is that it is less readable. In either case you mean "a is undefined", but to understand that void 0 evaluates to undefined you're relying on trivia.

The terseness is why minifiers tend to use it, but their output is not exactly an example of readable code (quite the contrary actually).

rwaldron commented 11 years ago

+1

(to @pluma's argument)