3

I have been trying to find an explanation as to why the expression Boolean(false)+[] evaluates to "false" in JavaScript. I can see that the + operator can be concatenation or addition depending on the types or the left and right hand, but can't seem to find a good explanation as to how does the empty array literal [] turn into string in this case.

I was looking at the following:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Addition

https://tc39.es/ecma262/#sec-addition-operator-plus or section "12.8.3 The Addition Operator (+)"

Balage
  • 174
  • 3
  • 11
  • What's the point of venturing into this kind of code, apart from wanting to make it even more twisted to understand? – Mister Jojo Oct 22 '20 at 21:14
  • I was writing up an answer, but what @Ivar just posted is more coherent. I might add to look at https://www.ecma-international.org/ecma-262/5.1/#sec-9 which is the type conversion part of the spec, which explains why it coerces arrays to a string, instead of a number for example. – AdamExchange Oct 22 '20 at 21:18
  • To add to @Ivar, I assume this decision was made because in most languages, strings are usually represented as an array of chars and so the intuition is that the "middle ground" between a bool and an array is a string. – Dylan Kerler Oct 22 '20 at 21:18

0 Answers0