[thelist] [JavaScript] typeof() and properties of nonexistent array keys

Paul Bennett Paul.Bennett at wcc.govt.nz
Thu Feb 8 15:55:00 CST 2007


Hi all,

Accessing a nonexistent property, OR a nonexistent array key of an object using typeof() executes successfully and returns 'undefined'
Eg:

Var myObject = new Object();
Alert(typeof(myObject.myUndefinedProperty) + ' | ' typeof(myObject[0]));

However, accessing and undefined property of an undefined array key causes the typeof() function to error out ('myObject[0] has no properties')and the script to stop executing
Eg:
Var myObject = new Object();
Alert(typeof(myObject[0].myUndefinedProperty));

My question is how is this different to trying to get the type of a non existent array key? After all, myObject has no properties when initialized, as I haven't given it any. What is it about the use of typeof in the second example that causes an error?

Paul



More information about the thelist mailing list