I wanted to add the elements of an array into another, so I tried this:
[1,2] + [3,4]
It responded with:
\"1,23,4\"
In JavaScript, the binary addition operator (+
) performs both numerical addition and string concatenation. However, when it's first argument is neither a number nor a string then it converts it into a string (hence "1,2
") then it does the same with the second "3,4
" and concatenates them to "1,23,4
".
Try using the "concat" method of Arrays instead:
var a = [1, 2];
var b = [3, 4];
a.concat(b) ; // => [1, 2, 3, 4];