Javascript Array Value Is Undefined ... How Do I Test For That
Solution 1:
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.
Solution 2:
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.
Solution 3:
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)
Solution 4:
There are more (many) ways to Rome:
//=>considering predQuery[preId] is undefined:
predQuery[preId] === undefined; //=> trueundefined === predQuery[preId] //=> true
predQuery[preId] || 'it\'s unbelievable!'//=> it's unbelievablevar isdef = predQuery[preId] ? predQuery[preId] : null//=> isdef = null
cheers!
Solution 5:
Check for
if (predQuery[preId] === undefined)
Use the strict equal to operator. See comparison operators
Post a Comment for "Javascript Array Value Is Undefined ... How Do I Test For That"