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?