I wanted to add the elements of an array into another, so I tried this:
[1,2] + [3,4]
It responded with:
\"1,23,4\"
would be nice if you could overload operators in JavaScript but you can't: Can I define custom operator overloads in Javascript? you can only hack the "==" operator which converts to strings before comparing: http://blogger.xs4all.nl/peterned/archive/2009/04/01/462517.aspx
It looks like JavaScript is turning your arrays into strings and joining them together. If you want to add tuples together, you'll have to use a loop or a map function.
It is because, + operator assumes that the operands are string, if they are not numbers. So, it first converts them to string and concats to give the final result , if its not a number. Also, it does not support arrays.
It's doing exactly what you asked it to do.
What you're adding together are array references (which JS converts to strings), not numbers as it seems. It's a bit like adding strings together: "hello " + "world"
= "hello world"
Some answers here have explained how the unexpected undesired output ('1,23,4'
) happens and some have explained how to obtain what they assume to be the expected desired output ([1,2,3,4]
), i.e. array concatenation. However, the nature of the expected desired output is actually somewhat ambiguous because the original question simply states "I wanted to add the elements of an array into another...". That could mean array concatenation but it could also mean tuple addition (e.g. here and here), i.e. adding the scalar values of elements in one array to the scalar values of the corresponding elements in the second, e.g. combining [1,2]
and [3,4]
to obtain [4,6]
.
Assuming both arrays have the same arity/length, here is one simple solution:
const arr1 = [1, 2];
const arr2 = [3, 4];
const add = (a1, a2) => a1.map((e, i) => e + a2[i]);
console.log(add(arr1, arr2)); // ==> [4, 6]
The +
operator is not defined for arrays.
What happens is that Javascript converts arrays into strings and concatenates those.
Since this question and consequently my answer is getting a lot of attention I felt it would be useful and relevant to have an overview about how the +
operator behaves in general also.
So, here it goes.
Excluding E4X and implementation-specific stuff, Javascript (as of ES5) has 6 built-in data types:
Note that although typeof
somewhat confusingly returns object
for Null and function
for callable Objects, Null is actually not an Object and strictly speaking, in specification-conforming Javascript implementations all functions are considered to be Objects.
That's right - Javascript has no primitive arrays as such; only instances of an Object called Array
with some syntactic sugar to ease the pain.
Adding more to the confusion, wrapper entities such as new Number(5)
, new Boolean(true)
and new String("abc")
are all of object
type, not numbers, booleans or strings as one might expect. Nevertheless for arithmetic operators Number
and Boolean
behave as numbers.
Easy, huh? With all that out of the way, we can move on to the overview itself.
Different result types of +
by operand types
|| undefined | null | boolean | number | string | object |
=========================================================================
undefined || number | number | number | number | string | string |
null || number | number | number | number | string | string |
boolean || number | number | number | number | string | string |
number || number | number | number | number | string | string |
string || string | string | string | string | string | string |
object || string | string | string | string | string | string |
* applies to Chrome13, FF6, Opera11 and IE9. Checking other browsers and versions is left as an exercise for the reader.
Note: As pointed out by CMS, for certain cases of objects such as Number
, Boolean
and custom ones the +
operator doesn't necessarily produce a string result. It can vary depending on the implementation of object to primitive conversion. For example var o = { valueOf:function () { return 4; } };
evaluating o + 2;
produces 6
, a number
, evaluating o + '2'
produces '42'
, a string
.
To see how the overview table was generated visit http://jsfiddle.net/1obxuc7m/