0

The expression [][[]] evaluates to undefined in JavaScript. My understanding of this was that the compiler sees the second set of [...] and interprets that to be an array subscript operator (because you can't have two arrays next to each other).

So the compiler knows that the inner expression, [], must be an index, and so after evaluating it, it coerces it to a number. Number([]) evaluates to 0, and so we have [][0], which is undefined.

However, [1][[]] does not evaluate to 1 as I would expect, but rather to undefined suggesting that in this case (or maybe also in the previous case), [] isn't being coerced to a number. It seems that I must use the unary + to force the type coercion:

[1][+[]] // returns 1

So if the inner [] in the expression [][[]] is not being coerced to a number, then why does that expression evaluate to undefined?

pushkin
  • 9,575
  • 15
  • 51
  • 95
  • Similar topic: https://stackoverflow.com/questions/7202157/why-does-return-the-string-10?rq=1 – Marty Mar 15 '18 at 23:11
  • Possible duplicate of [Why does ++\[\[\]\]\[+\[\]\]+\[+\[\]\] return the string "10"?](https://stackoverflow.com/questions/7202157/why-does-return-the-string-10) – Ele Mar 15 '18 at 23:11
  • 1
    You mention: *and so after evaluating it, it coerces it to a number*, which isn't the case. Instead it tries to coerce it to a *string*, which for an empty array is just `""`. Since `[1]` has no `""` field, the result is `undefined`. You can verify that by evaluating `[1][[0]]` to get `1` – CRice Mar 15 '18 at 23:12
  • Ohh, all keys are coerced to strings. I knew that was true for `Object`s, but forgot that arrays are special `Object`s. Feel free to post an answer. – pushkin Mar 15 '18 at 23:14
  • I'm on the fence about whether the linked question is a proper duplicate. It's certainly related, but my question is more specific. Reading the accepted answer didn't answer my question. I don't believe he called out the fact that array subscripts are coerced into strings. – pushkin Mar 15 '18 at 23:19
  • [Specification](//tc39.es/ecma262/#sec-property-accessors-runtime-semantics-evaluation), [documentation](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Property_Accessors#property_names). Related: [Are functions valid keys for javascript object properties?](/q/10858632/4642212). – Sebastian Simon Dec 20 '21 at 20:44

2 Answers2

1

The faulty assumption was that the expression that evaluates to the index is coerced to a number. It is in fact coerced to string as are all object keys (except for Symbols, which stay Symbols).

Thus, [1][[]] turns into [1][""], and since the "" property doesn't exist on the array, we get undefined instead of 1.

pushkin
  • 9,575
  • 15
  • 51
  • 95
-1

You are trying to get data with object.property.property so objects have properties, yes true but properties dont have properties, so it makes undefined. Think like this;

let myArray = [1, 2, 3];
console.log(myArray['length']);
// -> 3
console.log(myArray['length']['length']);
// -> undefined

And there i think there is a problem, [][[]] These brackets doesnt affect the code. Try to result it with some Codes;

let myArray = [[[1, 2, 3], 2, 3], 2, 3];
console.log(myArray[0][[0]]);
// -> [1, 2, 3]
console.log(myArray[0][0]);
// -> [1, 2, 3]
n3pixowe
  • 122
  • 10
  • 1
    I'm not getting data with `object.property.property`. I have one array `[]` and I'm accessing one property on it whose key is `[]`. Second, the outer `[]` brackets do affect my code. `[][]` won't compile. I need to put a key in there. – pushkin Mar 20 '18 at 14:19