Adding two arrays | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Adding two arrays

What's the point of using an add "+" operator instead of concatenation method on arrays. There a lot of examples in JavaScript coding challenges.For me it's useless.

12th Nov 2019, 7:06 PM
Vladyslav Semeniuk 🐸
Vladyslav Semeniuk 🐸 - avatar
3 Answers
+ 2
According to MDN web docs “it is strongly recommended that the assignment operators (+, +=) are used instead of the concat() method.” According to performance tests, the assignment operators are several times faster. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat
12th Nov 2019, 7:54 PM
Michael
Michael - avatar
0
Michael but last and first elements are merging into one and, as a result, you need to do additional operations to separate them.
12th Nov 2019, 10:47 PM
Vladyslav Semeniuk 🐸
Vladyslav Semeniuk 🐸 - avatar
0
Sorry, I misread your question. You can’t use the + operator for concatenation of arrays. The + operator performs a type conversion. It converts all arrays to strings. So the result of [1,2,3] + [4,5,6] would not be an array anymore. Only the concat method or the spread operator are suitable for merging arrays.
13th Nov 2019, 6:02 AM
Michael
Michael - avatar