3

I want to check this:

if ( typeof myVar != "undefined" && myVar != null )
    ...

In other words, I want to check if a variable has a defined value (including 0 or an empty string), but not undefined or null, which I interpret as valueless.

Do I have to do the two-part check each time or is there a handy shortcut?

devios1
  • 36,899
  • 45
  • 162
  • 260

5 Answers5

4

If you want to allow 0 and "" as valid values and you want to cover the case of the variable might not even be delcared, but don't consider null a valid value, then you have to specifically check for undefined and null like this:

if (typeof myVar !== 'undefined' && myVar !== null) 
   ...

A lot of values are falsey (they don't satisfy if (myVar) so you really have to conciously decide which ones you're testing for. All of these are falsey:

undefined
false
0
""
null
NaN

If you want to allow some, but not others, then you have to do a more specific test than if (myVar) like I've shown above to isolate just the values you care about.

Here's a good writeup on falsey values: http://www.sitepoint.com/javascript-truthy-falsy/.

If you know the variable has been declared and you just want to see if it's been initialized with something other than null, you can use this:

if (myVar != undefined) 
   ...

Using only the != instead of !== allows this to test for both undefined and null via type conversion. Although, I wouldn't recommend this because if you're trying to discern between falsey values, it's probably better to NOT let the JS engine do any type conversions at all so you can control exactly what it does without having to memorize all the type conversion equality rules. I'd stick with this to be more explicit:

if (typeof myVar !== 'undefined' && myVar !== null) 
   ...

If you want to know if it has any non-falsey value, you can of course do this (but that won't allow 0 or "" as valid values:

if (myVar) 
   ...
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 2
    This will throw a reference error when `myVar` is not initialized (either by setting a value or using `var myVar`). – Kenaniah Dec 01 '11 at 21:08
  • 1
    Also, `myVar != undefined` will produce the same result as `myVar != undefined && myVar != null`, since `null == undefined` is true. – Rob W Dec 01 '11 at 21:10
  • myvar==undefined returns true if my var is undefined or null, as does myvar==null. – kennebec Dec 01 '11 at 21:12
  • If myVar has been declared (even when not initialized), `myVar !== undefined` doesn't throw an error. See http://jsfiddle.net/jfriend00/fxM6m/. If it hasn't been declared and you want to test for that, then you do can use the typeof. I was under the impression that the variable was declared. In any case, I've updated my example. – jfriend00 Dec 01 '11 at 21:17
2

The 2-part method. If you don't check for the typeof first, you'll end up with a reference error.

Kenaniah
  • 5,171
  • 24
  • 27
1

If you know the context of myVar, you should be able to do this:

if (this.myVar != null) {
    ...
}
John Fisher
  • 22,355
  • 2
  • 39
  • 64
  • Aha! Yes of course, because `undefined == null`. How handy. :) As you say, the var must exist, but in my case this is true. – devios1 Dec 01 '11 at 21:54
  • This method should work whether `myVar` has been declared or not. If you know you are talking about a global variable you could also say `window.myVar != null` too. – nnnnnn Dec 01 '11 at 22:44
0

if myvar could be undefined, calling it without the typeof check will throw an error.

And if it is defined as null (like myvar= element.lastChild for an element with no children) you will miss catching it if you just use typeof.

kennebec
  • 102,654
  • 32
  • 106
  • 127
0

Well, null is a defined value... so if you want to make sure that the variable doesn't contain null, and isn't undefined you must do both checks.

Boundless
  • 2,444
  • 2
  • 25
  • 40