Speed of comparing to null vs undefined in JavaScript

null is a reserved keyword which cannot be overriden, so when you are doing a comparison against null, all you have to do is a single comparison.

However, when you are checking against undefined, the engine must do a type lookup and then a comparison, meaning that it is actually slightly more demanding.


If you need to actually check to see if something is undefined, you should use

if(typeof notSet == "undefined"){ }

Proof

Try it… and set something to null in your JavaScript console.

null = "will error";
// Errors with --> ReferenceError: invalid assignment left-hand side

However, if you try and do it with undefined, it won’t error. That is not to say that you can override undefined, because you can’t, but that undefined is its own primitive type.

The only real similarity between null and undefined, is that they can both be coerced into a boolean false.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)