How to access a numeric property?

This should work:

myObject["0"]

(myObject["propertyName"] is an alternative syntax for myObject.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:

A JavaScript identifier must start
with a letter, underscore (_), or
dollar sign ($); subsequent characters
can also be digits (0-9). Because
JavaScript is case sensitive, letters
include the characters “A” through “Z”
(uppercase) and the characters “a”
through “z” (lowercase).

Leave a Comment