I have an object like:
var myObject = { '0' : 'blue' };
Now, when I try to access the value of the key ‘0’ like:
myObject.0
…I am getting an error. (Maybe this is not the proper way?)
How can I access the value of a key that is a number (like the above)?
This should work:
(
myObject["propertyName"]is an alternative syntax formyObject.propertyName.)You’re getting the error because, in JavaScript, identifiers can’t begin with a numeral. From the Variables page at the Mozilla Developer Centre: