1

So I'm learning Arrays in JS (ES6), And I wondered why can't we access array elements with dot notation

var arr = [1,2,3,4,5];
console.log(arr[0]); //fine
console.log(arr['0']); //it works, some internal typecasting I guess
console.log(arr.0); // fails
console.log(arr.'0'); // fails too.

As JS philosophy says (not an official statement though) :

"Everything in JS is an Object"

Following that I expected it to work here, but then I tried implementing my own array using object. (don't know how it is actually implementation).

var obj = {0:1, 1:2, 2:3, 3:4, 4:5};
console.log(obj[0]); //works 
console.log(obj['0']); //works
console.log(obj.0); //fails 
console.log(obj.'0'); //fails

So even my own implementation, gives the same results as actual array implementation.

var obj = {"012":0, "a012":1};
console.log(obj.012); //fails
console.log(obj."012"); //fails
console.log(obj[012]); //surprisingly fails returns undefined.
console.log(obj["012"]); //works
console.log(obj.a012); //It works ah .

What's wrong in accessing objects with numeric keys using dot notation? I know the fact property should be a string, but "012" or any other string just containing numerical letters (no alphabets) is not valid. Why?

Ankit Kumar Ojha
  • 2,296
  • 2
  • 22
  • 30
  • 3
    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. ... but this seems to be only half of the problem :) – Aleksei Maide May 06 '18 at 05:50
  • ...and this expailns why `...[012]` returns `undefined` - since it's searching actually for `'12'` key and did not found that – skyboyer May 06 '18 at 05:52
  • In array the "index" means the memory offset from the start of the array, there is no mapping to object as it would involve extra overhead... – Aleksei Maide May 06 '18 at 05:52
  • 1
    @skyboyer nope, it is searching for "10" – Kaiido May 06 '18 at 05:52
  • as for `.` just deal with it. I suppose it have been done to make parsing code easier: dot + numbers after means fractional part of numbers, dot + alphanumeric means accessing property – skyboyer May 06 '18 at 05:55
  • @Kaiido yes, you are right. It's a number to be in octal system – skyboyer May 06 '18 at 05:56
  • Also, numbers starting with zero are treated as octal numbers... eg 012 is cast to 012+"" == "10" – Aleksei Maide May 06 '18 at 05:56
  • @AlekseiMaide thanks for explaining it, I have one more thing to ask why any key which is a string with only numerical characters cannot be accessed using dot notation? – Ankit Kumar Ojha May 06 '18 at 09:35
  • 1
    I guess the interpreter doesn't know how to parse it... must be ambigous... var obj = {"12":0}; typeof Object.keys(obj)[0]; //string... and obj.hasOwnProperty(12) //true ... the property clearly exists, my guess is it's simply a syntax limitation... – Aleksei Maide May 06 '18 at 09:58
  • @AlekseiMaide kinda make sense. – Ankit Kumar Ojha May 06 '18 at 19:53
  • 1
    @AnkitKumarOjha: It's pretty simply really: To use dot notation, the property name must be a valid **identifier** (i.e. something like a valid variable name (but also including keywords)). Numbers are not valid identifiers. – Felix Kling May 07 '18 at 18:13
  • @FelixKling If it is so, You have cleared all my doubts. Can you please share the sources for the comment you have written. – Ankit Kumar Ojha May 08 '18 at 09:05
  • 1
    Here is the spec: https://www.ecma-international.org/ecma-262/8.0/#sec-property-accessors. Note that the production rule is `MemberExpression . IdentifierName`. If you click on `IdentifierName` you can see its rules. – Felix Kling May 08 '18 at 17:06
  • @FelixKling Thanks a lot. – Ankit Kumar Ojha May 09 '18 at 14:21

0 Answers0