5

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?

Yukulélé
  • 15,644
  • 10
  • 70
  • 94

5 Answers5

13

Because 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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 1
    In other words, why language designers have chosen to forbid identifiers starting with number? – Yukulélé Jul 08 '15 at 20:38
  • 2
    I'd imagine it's not permitted to start with a number to reduce parsing ambiguities. If they allow `foo.4.bar`, you're going to get the tokens `foo`, `.`, `4.`, `bar`, so now they need the `4.` to be parsed relative to its context, which wouldn't give the expected result unless you did `foo.4..bar`. And then should decimal numbers be allowed? If so, how would `foo.4.2.bar` be parsed? Much simpler to not allow it. –  Jul 08 '15 at 20:55
  • @squint: Right, as I said :-) – Lightness Races in Orbit Jul 08 '15 at 20:57
  • I should have addressed the comment to @Yukulélé. Seems more detail was wanted. –  Jul 08 '15 at 21:01
2

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

PaulShovan
  • 2,140
  • 1
  • 13
  • 22
1

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.

Dan Luba
  • 114
  • 2
  • 9
  • My question is not only for arrays: With `myObj = {'abc': 'foo', '0': 'bar'}` I can access to `myObj.abc` but not `myObj.0`. however in this case `0` is a member of `myObj` – Yukulélé Mar 04 '20 at 13:16
  • You might guess that I am not a frequent Stack Overflow logger-inner. `myObj.0` is invalid because identifiers cannot begin with a number. This is an entirely different matter to array items being accessed with dot notation. It's a long time since I had anything to do with interpreters directly, but my guess is that identifiers generally can't begin with numerals because it would make the mechanics of interpretation more awkward. – Dan Luba Jul 13 '21 at 02:04
0

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.

Yukulélé
  • 15,644
  • 10
  • 70
  • 94
-2

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.

kieranpotts
  • 1,510
  • 11
  • 8
  • 1
    Repeating the question with the question mark stripped off is not an answer. – Lightness Races in Orbit Jul 08 '15 at 20:29
  • The question was "Why it's not possible to access [an] array element with dot syntax?" I've answered it clearly and succinctly in my first two sentences. – kieranpotts Jul 08 '15 at 20:33
  • 3
    The question was _"Why [is it] not possible to access [an] array element with dot syntax?"_ and your so-called clear and succinct answer is _"It is not possible to access an array element with dot syntax"_? Good job, I guess... – Lightness Races in Orbit Jul 08 '15 at 20:35