Why it's not possible to access to array element with dot notation?
var arr = ['Apple', 'Mango', 'Pineapple', 'Orange', {name: 'Banana', color: 'yellow'}];
console.log( arr[0] ); // "Apple"
console.log( arr.0 ); // "Apple"
console.log( arr.3 ); // "Orange"
console.log( arr[4].name ); // "Banana"
console.log( arr.4.color ); // "yellow"
In other words, why language designers have chosen to forbid identifiers starting with number?
Why it's not possible to access to array element with dot notation?
var arr = ['Apple', 'Mango', 'Pineapple', 'Orange', {name: 'Banana', color: 'yellow'}];
console.log( arr[0] ); // "Apple"
console.log( arr.0 ); // "Apple"
console.log( arr.3 ); // "Orange"
console.log( arr[4].name ); // "Banana"
console.log( arr.4.color ); // "yellow"
In other words, why language designers have chosen to forbid identifiers starting with number?
Share Improve this question edited Feb 27, 2020 at 12:02 Yukulélé asked Jul 8, 2015 at 20:22 YukuléléYukulélé 17.1k11 gold badges76 silver badges103 bronze badges 5 |5 Answers
Reset to default 13Because identifiers are not allowed to start with numbers, and the y
in x.y
is an identifier.
Why is the y
in x.y
an identifier? No idea. Ask the language designers on the appropriate mailing list or in an AMA session. I'd guess that it makes both language specification and interpretation wildly easier.
according to javascript's nature an object's property name can be defined as following...
An object property name can be any valid JavaScript string, or anything that can be converted to a string, including the empty string. However, any property name that is not a valid JavaScript identifier (for example, a property name that has a space or a hyphen, or that starts with a number) can only be accessed using the square bracket notation. This notation is also very useful when property names are to be dynamically determined (when the property name is not determined until runtime). more...
As array's property name starts with number it can only be accessed using square([])
brackets
Property names must be strings. This means that non-string objects cannot be used as keys in the object. Any non-string object, including a number, is typecasted into a string via the toString method.
So, in case of dot notation it looks for a string inside of the object and it never casts the value given after dot(.) notation. For this reason obj.1
is not valid and actually does not exist. On the other hand, in case of square([]) brackets the value is always casted into string to find the property
This is an old question, but it seems to have been recently edited, and I don't think any of the previous answers really got to the heart of it.
Firstly, "Why language designers have chosen to forbid identifiers starting with number?" is not "Why it's not possible to access to array element with dot notation?" in other words. They are wholly different questions.
Dot notation is for accessing object members only. An array element is not an object member.
You could visualize an array like this:
array = {elements: ['element_1', 'element_2'], length: f(), push: f()...}
You can see that you would not be able to access 'element_1' using array.0
, even if numeric identifiers were allowed. Moreover, the elements 'member' is a special kind of affair that the interpreter handles only through bracket notation (to the best of my knowledge).
kieranpotts' answer seems to have been a perfectly serviceable but misunderstood one, to me - perhaps it was downvoted because the idea that elements are not properties was not explicit enough.
Dot notation is for accessing object members only. An array element is not an object member.
array = {elements: ['element_1', 'element_2'], length: f(), push: f()...}
You can see that you would not be able to access 'element_1' using array.
Dot syntax is used only to access the properties of an object. It cannot be used to access the elements of an array.
Bracket notation [] is used to access the elements of an array. And bracket notation can be used as an alternative means of accessing the properties of objects. But contrast, dot notation is only for object properties.
your_tuple.0
,your_tuple.1
, etc. – Sebastian Simon Commented Jan 29, 2022 at 6:18