This question is about JavaScript language fundamentals. Why we can use dot (.) operator to access an object’s elements but not for array elements?
In other words why myArray.0 is not valid in JavaScript?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A javascript property name accessed with the dot notation cannot start with a number. That’s just part of the javascript syntax rules/grammar. So, even if dot notation could work for arrays, it won’t because array indexes are numbers and property names that start with numbers cannot be accessed with dot notation.
For arrays, you can obviously use:
But, that also works for object properties that start with a number like:
And, you cannot do:
even when
xis an object and"12"is a legitimate property on the object. You would have to usex["12"]to access it.