I was looking at some legacy code, and I noticed the following code (simplified here):
const age = [ 33 ];
const year = 2021;
const yearOfBirth = year - age; // gives the number 1988
In other words, number - [number]
is of type number
If, however I change it to
const age = [ 33, 44 ];
The subtraction doesn't work and I get NaN
. This makes sense to me.
Why is the first operation valid? Where is such thing documented?
PS: I'm aware a similar thing holds for string[]
, since String.prototype.toString()
is equivalent to String.prototype.join("")
. But I don't know why this holds for numbers.