10

I was having some issues in my conditions concerning undefined variables. What is, to sum it up, the best way to check if a variable is undefined?

I was mainly struggling with

x === undefined

and

typeof x === 'undefined'
DonJuwe
  • 4,477
  • 3
  • 34
  • 59
  • 2
    possible duplicate of [Detecting an undefined object property](http://stackoverflow.com/questions/27509/detecting-an-undefined-object-property) – Anatoli Feb 18 '15 at 12:24

4 Answers4

12

You can use both ways to check if the value is undefined. However, there are little nuances you need to be aware of.

The first approach uses strict comparison === operator to compare against undefined type:

var x;
// ...

x === undefined; // true

This will work as expected only if the variable is declared but not defined, i.e. has undefined value, meaning that you have var x somewhere in your code, but the it has never been assigned a value. So it's undefined by definition.

But if variable is not declared with var keyword above code will throw reference error:

x === undefined // ReferenceError: x is not defined 

In situations like these, typeof comparison is more reliable:

typeof x == 'undefined' // true

which will work properly in both cases: if variable has never been assigned a value, and if its value is actually undefined.

dfsq
  • 191,768
  • 25
  • 236
  • 258
2

I guess both, depending on what you're testing? If it's a property, I'd always use x === undefined, since it's clearer (and it seems faster).

As others said, x === undefined won't work if x is not declared. Personally, I find that an even better reason to use it, since normally I shouldn't be checking if a variable is declared at all — it would usually be a sign of a coding mistake.

I've seen a lot of the other version for testing arguments — f = function(x) {if (typeof x == 'undefined') …} — if I'm code-reviewing code like this I'll tell them to change it. We know for a fact the variable is declared, and making an habit of writing it that way increases the chance you'll waste time chasing typo bugs.

The main exception is when you're trying to check if a component or library was loaded or initialized correctly. if (typeof jQuery == 'undefined') … makes sense. But in the medium term, all this should become modules anyway, in which case the typeof test should in my opinion be phased out as harmful.

(Also, personally, I prefer if (window.jQuery === undefined) for that case too. It's not portable for isomorphic code, though.)

1
x === undefined

does not work if variable is not declared. This returns true only if variable is declared but not defined.

Better to use

typeof x === 'undefined'

Anurag Peshne
  • 1,547
  • 12
  • 29
  • @dfsq Actually I did, it did not work on chrome console. Am I missing something? – Anurag Peshne Feb 18 '15 at 12:27
  • 1
    There is a little moment. Check my answer. It didn't work in your case because variable `x` is not defined. If it has been defined like `var x`, you can check if it's `undefined` with `=== undefined`. – dfsq Feb 18 '15 at 12:28
  • @dfsq: This is new for me. Edited my answer to incorporate it. – Anurag Peshne Feb 18 '15 at 12:32
0

You could use any of them. If you would like to check if a variable is undefined or null you could use:

x == null

This results in true if x is undefined or null.

John
  • 2,043
  • 5
  • 28
  • 49