Why is [1,2] + [3,4] = “1,23,4” in JavaScript?

前端 未结 13 1655
小蘑菇
小蘑菇 2020-11-22 16:56

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\"
         


        
13条回答
  •  长情又很酷
    2020-11-22 17:28

    The + concats strings, so it converts the arrays to strings.

    [1,2] + [3,4]
    '1,2' + '3,4'
    1,23,4
    

    To combine arrays, use concat.

    [1,2].concat([3,4])
    [1,2,3,4]
    

提交回复
热议问题