JavaScript Array + Array = String?

JavaScript数组字符串+数组= ?

问题 (Question)

When you try to add an Array to another Array in JavaScript, it turns it into a String. Typically when this operation is performed in another languages, the lists are combined.


[1, 2] + [3, 4] = "1,23,4"


[1, 2] + [3, 4] = [1, 2, 3, 4]

I'm not trying to start a flame war about which language is better. But JavaScript's makes no sense to me. Is there a logical reason for why JavaScript does this? Or is it just a quirk of the language?



[1, 2] + [3, 4] = "1,23,4"


[1, 2] + [3, 4] = [1, 2, 3, 4]


最佳答案 (Best Answer)

This is how the Addition Operator (+) is defined..

The + operator does not have a "special meaning/overload" for other value types (like arrays) and the behavior in ECMAScript is independent of the behavior in any other language.

The production AdditiveExpression : AdditiveExpression + MultiplicativeExpression is evaluated as follows:

  1. Let lref be the result of evaluating AdditiveExpression.
  2. Let lval be GetValue(lref).
  3. Let rref be the result of evaluating MultiplicativeExpression.
  4. Let rval be GetValue(rref).
  5. Let lprim be ToPrimitive(lval).
  6. Let rprim be ToPrimitive(rval).
  7. If Type(lprim) is String or Type(rprim) is String, then Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)
  8. Return the result of applying the addition operation to ToNumber(lprim) and ToNumber(rprim). See the Note below 11.6.3.

The conversion in this case is rather subtle, but is the result of the array values, being Objects without a corresponding primitive type, having "toString" invoked as a result of the [ToPrimitive] operation. As such the rule application is approximately as follows.

   [1,2] + [3,4]
-> [1,2].toString() + [3,4].toString()     // Rules #5 and #6
-> "1,2" + "3,4"                           // Rule #7
-> "1,23,4"

这就是加法操作符(+)定义. .


生产AdditiveExpression:AdditiveExpression + MultiplicativeExpression评估如下:

  1. 让lref AdditiveExpression评估的结果。
  2. 让lval GetValue(lref)。
  3. 让rref MultiplicativeExpression评估的结果。
  4. 让rval GetValue(rref)。
  5. 让lprim ToPrimitive(lval)。
  6. 让rprim ToPrimitive(rval)。
  7. 如果类型(lprim)是字符串或类型(rprim)是字符串,然后返回的结果的字符串连接ToString(lprim)其次是ToString(rprim)
  8. 应用加法操作的结果返回当时(lprim)和当时(rprim)。请参见下面的说明11.6.3。


   [1,2] + [3,4]
-> [1,2].toString() + [3,4].toString()     // Rules #5 and #6
-> "1,2" + "3,4"                           // Rule #7
-> "1,23,4"

答案 (Answer) 2

Yes, JavaScript(JS) has quite a few 'quirks' to put it mildly. To get the behavior you desire, you can use the concat method. [1, 2].concat([3, 4])

The source of the confusion is, as Douglas Crockford says in his excellent book, "JavaScript: The Good Parts" arrays in JS are "Phony Arrays", (they do not do a linear allocation of memory among other things). They are actually objects with Array like characteristics which they inherit from Array.prototype. This is both good and bad, so beware.

In fact he puts JS arrays in the "Awful Parts" of JS section, where all problematic things in JS which cannot be easily avoided, are relegated to.

是的,JavaScript(JS)相当多的“怪癖”说得婉转些。得到你想要的行为,您可以使用concat方法。[1, 2].concat([3, 4])