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


The JavaScript's + operator is used to add two numbers or join two strings. However, use the contact() method to join two arrays to get a new one. For example,

[50, 70].concat([90, 100])

The above prints,

[50, 70, 90, 100]

Let’s see your example. The + operator concats strings, and converts the arrays to strings −

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

Or as mentioned above, use concat(),

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

Updated on: 24-Jun-2020

55 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements