0

Possible Duplicate:
javascript undefined compare

This always confuses me and I can never find a definantive answer, nor does it seem when I try one or the other does it conclusively show me that one way or the other works or works better then the other.

I see people do:

typeof system_obj.alpha !== 'undefined'
!typeof system_obj.alpha == 'undefined'
typeof system_obj.alpha != 'undefined'

but in any of the cases, I always end up with a false positive now and again. So my question is, when I want to know wether not a string isn't null, or undefined or if I want to check to see that the type of is not something then do something based on that I always end up with something like

if(typeof system_obj.alpha == 'undefined'){/*ignore*/}else
{
  //code
}

How do I do that properly without having to use the if-else logic like I end up doing anyway

Community
  • 1
  • 1
chris
  • 36,115
  • 52
  • 143
  • 252
  • I'm confused... what's your problem? If you just want to check whether a variable (or property) is not `null` or `undefined`, you can compare against `null`: `system_obj.alpha != null`. – Felix Kling Jan 24 '13 at 21:18
  • Do you have an example of a false positive? Also, could you wrap the second one in parens and achieve the same thing? ie `!(typeof system_obj.alpha === 'undefined')` – beezir Jan 24 '13 at 21:18
  • @camus Declaring a variable with `var x` *does* mean that `x` is equal to `undefined`. It is *not* equal to `null`. (I think you meant to draw a distinction between variable identifiers that are *not defined* and extant variables that are equal to `undefined`.) – apsillers Jan 24 '13 at 21:24
  • @FelixKling in this case I am wanting to make sure the property is not undefined before continuing. However this is overall a generalized question to ask how to properly see if something is not a specific thing before continuing with javascript, be it string comparison such as `"yes" !== "no"` lame example but hopefully point achieved – chris Jan 24 '13 at 21:24
  • You won't be able to avoid the `if` statement, but you can reverse the logic: `if(typeof system_obj.alpha !== 'undefined') { /* code */ }` or you assign a default value to the variable if appropriate `system_obj.alpha = system_obj.alpha == null ? defaul_value : system_obj.alpha; // code`. It's not really something you can generalize (apart from saying use `typeof`). – Felix Kling Jan 24 '13 at 21:26
  • *if(typeof system_obj.alpha == 'undefined'){...}else* should be the exact same as *if(typeof system_obj.alpha !== 'undefined')* I don't see where a false positive would get in the mix... Like @beezir I'd be interested to see an example. – Christophe Jan 24 '13 at 21:53

2 Answers2

1

use system_obj.alpha === void 0 it's the fastest way to check if a variable is undefined, and it's reliable. Here's the comparison.

http://jsperf.com/typeof-vs-undefined-check/3

hobberwickey
  • 6,118
  • 4
  • 28
  • 29
1

Just do the following:

if (typeof systemObj !== 'undefined') {
  ... do something
}

To test that the object is truthy (and NOT undefined, null, false etc.) do the following:

if (systemObj) {
    ... do something (will throw error is object does not exist)
}

The way your example appears above you can do the following:

 if (systemObj.hasOwnProperty('alpha') && systemObj.alpha) {
     ... do something
 }

That will check to make sure alpha exists and isn't falsey.

Outside the scope of the question I recommend the following:

  • Use camel case.

  • Don't write your if statements that way.

  • Check out http://addyosmani.com/blog/ or similar resources to learn how to write better JavaScript.

N..
  • 119
  • 1
  • 2
  • 7