Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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" 

What is going on?

like image 651
okeen Avatar asked Aug 19 '11 16:08

okeen


People also ask

What is meant by ${} in JavaScript?

In Javascript the ${} is used to insert a variable to a string. var foo = "cheese"; console.

What is the function of === in JavaScript?

Using Strict Equal (===) operator: In JavaScript, '===' Operator is used to check whether two entities are of equal values as well as of equal type provides a boolean result. In this example, we use the '===' operator. This operator, called the Strict Equal operator, checks if the operands are of the same type.

What would be the result of 2 5 3 in JavaScript?

What would be the result of 2+5+”3″? Since 2 and 5 are integers, they will be added numerically. And since 3 is a string, its concatenation will be done. So the result would be 73.


1 Answers

The + operator is not defined for arrays.

What happens is that Javascript converts arrays into strings and concatenates those.

 

Update

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:

  1. Undefined
  2. Null
  3. Boolean
  4. Number
  5. String
  6. Object

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/

like image 77
Saul Avatar answered Sep 27 '22 20:09

Saul