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

JavascriptObject Oriented ProgrammingFront End Technology

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]
raja
Published on 01-Mar-2018 10:13:49
Advertisements