javascript - Why does [] + [] return an empty string? -


lately have been experimenting node.js , found out javascript has syntactic logic not wrap head around. example not understand , wondering whether random javascript fact or if there logic it.

the plus sign either arithmetic plus or string concatenation. empty arrays converted empty strings in case of [] + [].

the array's tostring method return 1 string comma separated list of of array's elements.

from the mdn reference above:

javascript calls tostring method automatically when array represented text value or when array referred in string concatenation.

the same idea of automatic type conversion why true + true === 2, , type conversion basis of many tricky javascript quizzes this one.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -