Problem with Javascript object and accessing property which exists

Posted by Newbie on Stack Overflow See other posts from Stack Overflow or by Newbie
Published on 2010-04-06T17:50:08Z Indexed on 2010/04/06 17:53 UTC
Read the original article Hit count: 116

Filed under:
|
|
|

I have something like this:

var test = {};

function blah() {
   test[2] = 'filled';
}

console.log(test);
//result  test -> 2:"filled"

console.log(test[2]);
//result  undefined

I don't understand why I'm getting 'undefined' in the second instance when according to the first instance, the property of that object clearly exists!

Does anyone have any ideas?

Thanks

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about objects