13

I'm a bit confused about how best to check if a variable is undefined or not in javascript. I've been doing it like this:

myVar === undefined;

But is it better in all cases to use typeof instead?

typeof myVar === undefined;

And what about the use of undefined vs "undefined", which I've also seen?

larryq
  • 15,713
  • 38
  • 121
  • 190
  • 5
    `typeof` returns a string, so you should compare its result to `"undefined"` if you use the strict comparison operator. – Frédéric Hamidi Feb 26 '13 at 16:14
  • 1
    If you only want to know if `myVar` has a non-falsy value, `!` would suffice. (e.g. `if(!myVar)` – marekful Feb 26 '13 at 16:15
  • @ Frédéric Hamidi-- thank you for the explanation. Do you use (or recommend) using typeof in most circumstances? In all circumstances? I ask because I've seen `myVar === undefined` used in reputable books, which is why I've been using it. – larryq Feb 26 '13 at 16:17
  • 2
    @larryq, `myVar === undefined` is not the same thing as `typeof myVar === "undefined"`. The first syntax will throw an exception if `myVar` does not exist in the current scope. See the potential duplicate for the full story. – Frédéric Hamidi Feb 26 '13 at 16:18
  • 1
    This is *not* a duplicate of 'typeof === "undefined" vs. != null'. The similarity is superficial. Please read carefully before you cast those Close votes, folks. – dgvid Feb 26 '13 at 20:05
  • @dgvid, how not? `!= null` and `!= undefined` behave the same, and IMHO the answers in the duplicate match this question quite well. – Frédéric Hamidi Feb 26 '13 at 23:39
  • `!= null` and `!= undefined` *often* behave the same way, but not always. And although it's true that the two questions have nearly duplicate answers, most people don't arrive at Stack Overflow with answers, they come here with questions. – dgvid Feb 27 '13 at 14:43

3 Answers3

20

This is the best way to check -- totally foolproof:

typeof myVar === "undefined"

This is OK, but it could fail if someone unhelpfully overwrote the global undefined value:

myVar === undefined;

It has to be said that ECMAScript 5 specifies that undefined is read-only, so the above will always be safe in any browser that conforms.

This will never work because it ends up comparing "undefined" === undefined (different types):

typeof myVar === undefined;
Jon
  • 428,835
  • 81
  • 738
  • 806
  • Why is this an accepted answer? AFAIK, attempting to access an undefined memory space will always throw an ReferenceError in both ES5 and ES6: https://jsfiddle.net/Troop4Christ/on2oynvz/ and http://www.es6fiddle.net/ijls28o5/ – RavenHursT Jan 19 '16 at 19:12
  • @RavenHursT: That's not the point of this question -- although granted, the example code given in the question is not the best. It's more like what happens [here](https://jsfiddle.net/on2oynvz/1/). Regarding `ReferenceError`, there is at least [this question](http://stackoverflow.com/q/9981104/50079) that covers it which I know about because I have an answer there too. – Jon Jan 20 '16 at 10:14
2

This test would always work as expected:

typeof a === 'undefined'

Since the value of undefined can be changed, tests like these aren't always reliable:

a = {}
a.b === undefined

In those cases you could test against void 0 instead:

a.b === void 0
// true

However, this won't work for single variable tests:

a === void 0 // <-- error: cannot find 'a'

You could work around that by testing against window.a, but the first method should be preferred.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

I believe that in the most common cases, e.g. when checking if a parameter is passed through a function, myVar === undefined is enough, as myVar will be always declared as a parameter

de3
  • 1,890
  • 5
  • 24
  • 39