42

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.

3
  • I have used (typeof(predQuery[preId])=='undefined') as my clause in my if statement. Commented Apr 20, 2010 at 6:58
  • @deceze ... I meant that, I have changed my code. I haven't edited the question however as that won't help future people with the same problem. Commented Apr 21, 2010 at 2:00
  • I see, sorry for the misunderstanding. :o) Commented Apr 21, 2010 at 2:03

7 Answers 7

77

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.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 comparing against the immutable type instead of the mutable undefined
This alone cannot distinguish between [undefined][0] and [][0]. If there's any chance that your array could be empty, you will likely encounter this scenario.
Why not array[index] === undefined
16

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.

Comments

9
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)

Comments

4

There are more (many) ways to Rome:

cheers!

const predQuery = {iExist: `hello world!`};
const preId = 'I-do-no-exist', exists = `iExist`;

console.log(predQuery[exists]); //=> hello world
console.log(predQuery[exists] === undefined); //=> false
console.log(predQuery[preId] === undefined); //=> true
console.log(undefined === predQuery[preId]) //=> true
console.log(predQuery[preId] || 'it\'s unbelievable!') //=> it's unbelievable
console.log(preId in predQuery); //=> false
console.log(exists in predQuery); //=> true
let isdef = predQuery[preId] ? predQuery[preId] : null;
console.log(isdef); //=> isdef? null
isdef = predQuery[preId] ?? `${preId} not in predQuery`;
console.log(isdef); //=> I-do-no-exist not in predQuery
.as-console-wrapper {
  max-height: 100% !important;
}

1 Comment

predQuery[preId] === undefined; does not work for me. An error is thrown.
2

Check for

if (predQuery[preId] === undefined)

Use the strict equal to operator. See comparison operators

5 Comments

almost - but best practice is as deceze says, compare typeof because that cannot be redefined as undefined can be.
Not foolproof, as undefined can be defined. Simply with window.undefined="lolz";
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.
ES5 enforces undefined as readonly.
Does not work for me. Error is thrown.
2

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

1 Comment

This does not work for me. yourArray[yourIndex] is evaluated, causing an error to be thrown. How come so many wrong answers? I guess JavaScript itself must have changed.
1

This code works very well

function isUndefined(array, index) {
    return ((String(array[index]) == "undefined") ? "Yes" : "No");
}

1 Comment

Why do you use == instead of === ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.