6 ms·
How can adding two empty arrays give you an empty string?
by najmlion 5y ago
How can adding two empty arrays give you an empty string?
- superice 5y agoThe + operator doesn’t work on arrays, but tries to cast the sides of it to a string. Arrays implement the toString function, which shows the toString of the individual values comma separated. An empty array returns an empty string. Which means that this is equivalent to empty string + empty string. [ ‘foo’, ‘bar ] + [ ‘baz’ ] would return ‘foo,barbaz’
- abdusco 5y agoThat's Javascript for you. Summing arrays calls `.toString()` on them, and two empty string gives you a new empty string. [].toString() === '' [] + [] === '' + '' === '' https://github.com/denysdovhan/wtfjs#adding-arrays https://github.com/denysdovhan/wtfjs#adding-arrays