Lately i have been experimenting with node.js and I found out that javascript has some syntactic logic that I could not wrap my head around. This is an example I do not understand and I was wondering whether this is just a random javascript fact or if there is any logic to it.
The plus sign is either arithmetic plus or string concatenation. The empty arrays are converted to empty strings in the case of [] + []
.
The Array's toString
method will return one string that is the comma separated list of all of the array's elements.
From the the MDN reference above:
JavaScript calls the toString method automatically when an array is to be represented as a text value or when an array is referred to in a string concatenation.
The same idea of automatic type conversion is why true + true === 2
, and type conversion is the basis of many tricky JavaScript quizzes like this one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With