-1

Something interesting about adding empty arrays in javascript was pointed out to me, and I have no idea why it works the way it does.

Adding empty arrays results in a string.

In other terms, [] + [] returns ""

I booted up my console to test it, and sure enough, it works. I further discovered that this behavior is not limited to empty arrays. Arrays of numbers, strings, arrays, and objects all turn into strings when added to any other array. Some examples are:

[1] + [] //returns "1"
[1] + [2] //returns "12"
[1,2,3] + [2] //returns "1,2,32"
[{foo:'bar'},'foo','bar'] + [] //returns "[object Object],foo,bar"

It also occurs with other objects, when added to anything else, but only if the object is on the right hand side. If it's on the left hand side, the object is turned into 0.

'foo' + {foo:'bar'} //returns "foo[object Object]"
1 + {foo:'bar'} //returns "1[object Object]"
{foo:'bar'} + 1 //returns 1
{foo:'bar'} + 'foo' //returns NaN

This happens unless I assign the object to a variable. If I use X = {foo:'bar'},

X + 'foo' //returns "[object Object]foo"

And the object is back to turning into a string.

I can understand why this sort of casting might happen in a == operator, but why does addition do this? Why does addition change arrays and objects (and yes, I know that arrays are objects, too) into other things?

ckersch
  • 7,507
  • 2
  • 37
  • 44
  • Bacause that's what JavaScript does. That's what (loose or weak?) typing is. – Reinstate Monica -- notmaynard Apr 17 '13 at 22:18
  • Is there an actual question to do with your homework or work here? This isn't really a discussion forum. – griegs Apr 17 '13 at 22:19
  • I agree on "that's what JavaScript" does. However, that is definitely not what loose typing is, it's just JavaScript's inherent quirkiness :) – Kenneth Apr 17 '13 at 22:19
  • "Why" questions about language behavior are out of scope for this site. Perhaps the language developers could provide an insight into their mindset, but we mere mortals must simply recognize and account for such behavior. – George Cummins Apr 17 '13 at 22:19
  • What would be the meaning of these operations? PHP does have a `+` operator for arrays, but it's basically the same as JS's `arr1.concat(arr2)` – Niet the Dark Absol Apr 17 '13 at 22:21
  • If you would like an open discussion on the "quirks" of a language then consider turning this into a community wiki – griegs Apr 17 '13 at 22:21
  • I'd think that, for arrays at least, [1,2,3] + [2,3,4] would result in [1,2,3,2,3,4]. – ckersch Apr 17 '13 at 22:22
  • And why not [3,5,7]? There are several possibilities depending on the types involved, so the language creator decided to limit the operator to strings and numbers. – bfavaretto Apr 17 '13 at 22:33
  • Element-wise addition seems specific to what's contained in the array. Combining arrays into a larger array seems like the intuitive way to add them. – ckersch Apr 17 '13 at 22:36
  • 1
    JavaScript type coercion can do a lot to produce unintuitive results, I've recently learned a lot more about this: http://stackoverflow.com/questions/15978204/what-are-javascripts-builtin-strings – Jason Sperske Apr 17 '13 at 22:55

3 Answers3

5

That operator is just for adding numbers or concatenating strings, if you use it with other types, they will be cast to number or string, depending on the case (that part is actually a bit complicated...).

From the language specification:

The addition operator either performs string concatenation or numeric addition.

As for why the language was designed that way, we can only guess, but Shadow Creeper shows a plausible reason in his answer: there would be several possibilities for its behavior on each different type, so it could be confusing.

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
1

This is called type coercion, this is how JavaScript is written. I recommend you the following read, to get a clear picture: http://blog.jeremymartin.name/2008/03/understanding-loose-typing-in.html

As a consequence, you shall avoid using non-type-safe equasion operators, i.e. == and !=. You shall use === and !== and force type conversions (parseInt(), etc.) where possible.

Adam Szabo
  • 11,302
  • 18
  • 64
  • 100
1

Trying to execute ( [1,2,3] + [1,2,3] ) is ambiguous. Should it result in [1,2,3,1,2,3] or [2,4,6] or string value "[1,2,3][1,2,3]"?

Javascript tries to convert things in ways that it thinks make sense. For a bit of an explanation about that see http://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/

Shadow Man
  • 3,234
  • 1
  • 24
  • 35