6

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?

5

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

In other words, why language designers have chosen to forbid identifiers starting with number?
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.
@squint: Right, as I said :-)
I should have addressed the comment to @Yukulélé. Seems more detail was wanted.
3

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

Comments

2

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.

2 Comments

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

2 Comments

code should be formated.
7 is a member of object obj = {x: 'foo', 7: 'bar'} but you can't acces it with obj.7
-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.

3 Comments

Repeating the question with the question mark stripped off is not an answer.
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.
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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.