Why does [][[]] evaluate to undefined?

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?

10 thoughts on “Why does [][[]] evaluate to undefined?”

  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];
    // -> 3
    // -> 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];
    // -> [1, 2, 3]
    // -> [1, 2, 3]
  2. 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.


Leave a Comment