0

*Edited

I recently stumbled upon an interesting as well as a peculiar problem with arrays in js.

enter image description here

As you can see above subtraction is giving me a number but addition gives me a string. I've also tried the same with multiplication and division and got number in both the cases.

Then I became more curious and tried with multi element arrays and here is the result

enter image description here

Now I really want to figure out how and why is this happening. Please help me on this.

Edit: I've added more examples and below are the results

enter image description here

As you can see above "1"+"1" is "11" which seems fine but [] returns an empty array and [1] returns 1 with key 0 and as we know arrays are also objects and that leads to confusion.

Abhinav
  • 238
  • 2
  • 12
  • 8
    subtraction coerces to a number, addition, being ambiguous with concatenation, follows a more complex coercion pattern, with a pref for strings. it's all well-documented though... – dandavis Apr 08 '19 at 18:23
  • 1
    When you are doing operations on array types, the `+` operator is being treated as concatenation instead of addition. – Isaac Vidrine Apr 08 '19 at 18:24
  • 1
    it can get worse :D example : `false-1-"1"*true+"2"-0 =-22` and a lot of other examples ^^ – jonathan Heindl Apr 08 '19 at 18:28
  • Possible duplicate of [Why is \[1,2\] + \[3,4\] = "1,23,4" in JavaScript?](https://stackoverflow.com/questions/7124884/why-is-1-2-3-4-1-23-4-in-javascript) – Heretic Monkey Apr 08 '19 at 18:44
  • @dandavis but please help me understand why [2] - [1] works and [1, 2, 3] - [4, 5, 6] not? – Abhinav Apr 08 '19 at 19:18
  • because that's `"2"-"1"` vs `"1,2,3"-"4,5,6"`, which doesn't work because of the commas. – dandavis Apr 08 '19 at 19:46

2 Answers2

0

When operating on elements in an array, the + operator performs a concatenation, not an addition.

For example, if you look at your case in which you try to do [1, 2, 3] + [4, 5, 6], the console treats the arrays as strings and joins the addend directly to the augend, giving a result of "1, 2, 34, 5, 6". The -, /, *, % operators perform arithmetic operations, which are integer operations rather than string or array operations, and cannot be performed on arrays. That is why that returns NaN.

If you're wondering why the numbers are added together as strings, it's because of type coercion. Concatenation is a string operation, not an integer operation, so the numbers are converted to a string.

If you want to append one array to another array, you can use .concat(). If you want to add each element (i.e. [0]1 + [0]2, ...), you can use a for loop. If you want to sum the elements in a list, use reduce with an accumulator.


Links: Explanation, Type conversion docs

Related tags: , , , ,

Jodast
  • 1,193
  • 1
  • 15
  • 32
0

Javascript arrays are objects, and arithmetic operator does not apply for them; + however performs concatenation. to join array objects, concat()

console.log("1" + "1") // = 11
console.log("1" - "1") // = 0

console.log(parseFloat("1") + parseFloat("1")) // = 2
console.log(parseFloat("1" + "1")) // = 11

console.log(eval("1") + eval("1")) // = 2
console.log(eval("1" + "1")) // = 11


var a = [1, 2, 3],
    b = [11, 22, 33, 44];
var c = a.concat(b);

console.log(c)
kr153
  • 284
  • 2
  • 9