Dot and Square Bracket Notation Doubts

Let’s consider the following array:

let a = [8, 13];

Doubt 1:
Why is a.length is the same as a["length"]? Can’t we just write a[length] and let the JavaScript engine do the type coercion as it does on a[1] like a['1']?

Doubt 2:
Which brings me to this one, why does the JavaScript engine like shown above convert 1 to '1' when properties’ names in an array are numbers?

Doubt1: An Array is an object with properties, the same as any other object in JavaScript. When you use a.length, you get the length property of the object. Using a['length'] also works, because it’s an object, so you can use either method to get the length but it looks nicer in this case when you can use the length property directly.

a[length] wouldn’t work unless length was a variable initialised as the string ‘length’. Length is not a keyword or reserved word in JavaScript as far as I’m aware. Also in your example a[2] doesn’t exist, surely you mean a[1]?

Doubt2: Type conversion can occur in a number of cases, Implicit type conversion. This should also clarify why a[length] wouldn’t work.

Yea, I meant a[1]. I’ve edited it above.

Sure, it could work that way. But that now makes it impossible to pass a variable. So for example, you couldn’t do this:

function phoeneticLookup (letter) {
  const lookup = {
    "a": "alpha",
    "b": "bravo",
    "c": "charlie",
    "d": "delta",
    "e": "echo",
    /* ....and so on */

  return lookup[letter];

If whatever is in the brackets gets converted to a string, then you can’t ever use variables there.

This already exists as the dot syntax, which is a simple, obvious syntax for the most common use case when dealing with objects: when you already know the name of the key, and want to look up the property/assign to it/whatever.

And you can’t do type coercion on a variable, that doesn’t make sense: the variable itself cannot have a type, it’s just a variable. The thing that’s assigned to the variable will have a type, but with what you’re saying, you can’t ever get to a point where that is checked.

Object keys in JS are strings (or symbols, but that’s not relevant here). That you can access and set them using integers is a necessary convenience.