views:

381

answers:

6

I am trying to test to see whether a Javascript variable is undefined.

You will see that I am not expecting the value of predQuery[preId] to be 'undefined' if I don't first get an alert saying "its unbelievable". But I often do, so I am guessing that my statement

 predQuery[preId]=='undefined') 

is not matching the undefined elements properly.

if((predQuery.length < preId) || (predQuery[preId]=="") || (predQuery[preId]=='undefined')){
alert("its unbelievable");
alert(predQuery[preId]);
queryPreds[variables] = preId;
queryObjs[variables] = objId;
predQuery[preId] = variables;
}
else {
    alert(predQuery[preId]);
var predIndex = predQuery[preId];
queryPreds[predIndex] = preId;
queryObjs[predIndex] = objId;
}

I can add more code if needed.

+1  A: 

Check for

if (predQuery[preId] === undefined)

Use the strict equal to operator. See comparison operators

rahul
almost - but best practice is as deceze says, compare typeof because that cannot be redefined as undefined can be.
Sky Sanders
Not foolproof, as undefined can be defined. Simply with `window.undefined="lolz";`
ItzWarty
It's impossible to write JavaScript that guards against every builtin being redefined (almost anything can be). I don't advocate making code less readable just to avoid redefinitions. `typeof` does have another use, to detect datatypes in cross-window scripting, but that doesn't apply to `undefined` anyway.
bobince
+3  A: 

array[index] == 'undefined' compares the value of the array index to the string "undefined".
You're probably looking for typeof array[index] == 'undefined', which compares the type.

deceze
+1 comparing against the immutable type instead of the mutable `undefined`
Sky Sanders
+1  A: 

try: typeof(predQuery[preId])=='undefined'
or more generally: typeof(yourArray[yourIndex])=='undefined'
You're comparing "undefined" to undefined, which returns false =)

ItzWarty
+2  A: 

You are checking it the array index contains a string "undefined", you should either use the typeof operator:

typeof predQuery[preId] == 'undefined'

Or use the undefined global property:

predQuery[preId] === undefined

The first way is safer, because the undefined global property is writable, and it can be changed to any other value.

CMS
+1  A: 

There are more (many) ways to Rome:

//=>considering predQuery[preId] is undefined:
predQuery[preId] === undefined; //=> true
undefined === predQuery[preId] //=> true
predQuery[preId] || 'it\'s unbelievable!' //=> it's unbelievable
var isdef = predQuery[preId] ? predQuery[preId] : null //=> isdef = null

cheers!

KooiInc
+2  A: 
predQuery[preId]=='undefined'

You're testing against the string 'undefined'; you've confused this test with the typeof test which would return a string. You probably mean to be testing against the special value undefined:

predQuery[preId]===undefined

Note the strict-equality operator to avoid the generally-unwanted match null==undefined.

However there are two ways you can get an undefined value: either preId isn't a member of predQuery, or it is a member but has a value set to the special undefined value. Often, you only want to check whether it's present or not; in that case the in operator is more appropriate:

!(preId in predQuery)
bobince