Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typeof !== "undefined" vs. != null

I often see JavaScript code which checks for undefined parameters etc. this way:

if (typeof input !== "undefined") {     // do stuff } 

This seems kind of wasteful, since it involves both a type lookup and a string comparison, not to mention its verbosity. It's needed because undefined could be renamed, though.

My question is:
How is that code any better than this approach:

if (null != input) {     // do stuff } 

As far as I know, you can't redefine null, so it's not going to break unexpectedly. And, because of the type-coercion of the != operator, this checks for both undefined and null... which is often exactly what you want (e.g. for optional function parameters).

Yet this form does not seem widespread, and it even causes JSLint to yell at you for using the evil != operator.

Why is this considered bad style?

like image 394
Derek Thurn Avatar asked Apr 24 '10 03:04

Derek Thurn


People also ask

Is undefined the same as null?

undefined and null variables oftentimes go hand-in-hand, and some use the terms interchangeably. Though, there is a difference between them: undefined is a variable that refers to something that doesn't exist, and the variable isn't defined to be anything. null is a variable that is defined but is missing a value.

Is null == undefined?

It means null is equal to undefined but not identical. When we define a variable to undefined then we are trying to convey that the variable does not exist . When we define a variable to null then we are trying to convey that the variable is empty.

What is the value of typeof undefined == typeof null?

Referencing undeclared variables usually results in a ReferenceError, except when using the typeof keyword. The typeof undefined is the string "undefined" — and undefined is a falsy value that is loosely equal to null but not to other falsy values.

Should I use null or undefined?

Only use null if you explicitly want to denote the value of a variable as having "no value". As @com2gz states: null is used to define something programmatically empty. undefined is meant to say that the reference is not existing. A null value has a defined reference to "nothing".


2 Answers

typeof is safer as it allows the identifier to never have been declared before:

if(typeof neverDeclared === "undefined") // no errors  if(neverDeclared === null) // throws ReferenceError: neverDeclared is not defined 
like image 51
seanmonstar Avatar answered Sep 28 '22 03:09

seanmonstar


If the variable is declared (either with the var keyword, as a function argument, or as a global variable), I think the best way to do it is:

if (my_variable === undefined) 

jQuery does it, so it's good enough for me :-)

Otherwise, you'll have to use typeof to avoid a ReferenceError.

If you expect undefined to be redefined, you could wrap your code like this:

(function(undefined){     // undefined is now what it's supposed to be })(); 

Or obtain it via the void operator:

const undefined = void 0; // also safe 
like image 25
Joey Adams Avatar answered Sep 28 '22 03:09

Joey Adams