What happens here is that [3] forced to a String type, which is just "3" . Then, in "3" * 3 value of String "3" converted to 3 . Finally, you get 3 * 3 , which is rated as 9 .
This behavior can be seen if you execute [3].toString() * 3 or "3" * 3 , which also gives you 9 .
So, the following steps are performed:
[3] * 3[3].toString() * 3"3" * 3Number("3") * 33 * 39
In the case of [1, 2] you end up with "1, 2" . But Number("1, 2") leads to a NaN , and a number times a NaN leads to NaN .
source share