1

Why does summing 2 arrays joins everything in a string, concatenating the first and last values:

  let array = [1, 2]

  let array2 = [3, 4]

  let array3 = array + array2

  console.log(array3) // 1,23,4
7
  • 2
    destroyallsoftware.com/talks/wat Commented Apr 12, 2022 at 22:34
  • javascript is dynamically typed and will attempt to coerce any variable to a suitable type for the operation at hand Commented Apr 12, 2022 at 22:34
  • When you try to add them, js reduces them to strings first. array2.toString() would result in "3,4". To concatenate arrays you could use the .concat method or you could use the spread operator Commented Apr 12, 2022 at 22:36
  • @pilchard the link refers to another question that is totally different for me, please read both question and note that they have totally different content. Perhaps both question do share a common accepted answer but each one has a totally different approach. If you don't want to answer, you don't have to; but allow others to do it. Same goes for sideshowbarker. I guess you try to help, but actually you don't. Commented Apr 12, 2022 at 23:54
  • @Victor I appreciate your concern but at it's heart this is just a coercion question and the linked duplicate covers that. There is even more discussion here Empty arrays seem to equal true and false at the same time including links and discussion of the relevant parts of the spec. I did upvote your answer as I felt it was the most concise of the three, but it could at least link to the docs if not the spec Commented Apr 13, 2022 at 0:09

3 Answers 3

4

That's just how JavaScript works. Both are objects, there's not a defined thing for + between an Array and an Array, so it does the default thing of converting to String and concatenating them (still as a String).

Use the Array.prototype.concat method to acheive your goal.

let array = [1, 2]

let array2 = [3, 4]

let array3 = array.concat(array2);

console.log(array3) // [1, 2, 3, 4]

Alternatively, you can use the spread operator:

let array = [1, 2]

let array2 = [3, 4]

let array3 = [...array, ...array2];

console.log(array3) // [1, 2, 3, 4]

Sign up to request clarification or add additional context in comments.

1 Comment

The OP didn't mention they were trying to concat them, merely asked why they were concatenated as strings.
3

Because the + operator acts diffently according to the type of the given operands.

  • If both operands are numbers, then a sum if performed.
  • If one of the operand is not number, then both operands are transformed into string (using .toString) and concatenated.

In your example you may notice that:

array + array2 == array.toString() + array2.toString()

For further information you may read the docs and the spec

Comments

1

Javascript tries to be smart and figure out what you are trying to do. An addition sign implies adding two strings, so Javascript goes ahead and converts the arrays to strings first for you. How nice, right?

What you want to do is implement the concat method:

const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.