6 Interesting finding about JavaScript

I found curios things about JavaScript and decided to share.

1. [1,2] + [3,4]?

Yes, “1,23,4”… And that is why — [1,2] + [3,4] = [1,2].toString() + [3,4].toString() OR [1,2].concat([3,4]). So addition of arrays works like addition of strings. This is related link.

2. a = {}; a+a

And the result will be “[object Object][object Object]”, because an object has toString method that by default returns [object Object].

3. {} + 2

Yes, this one also is interesting and the answer is 2.

4. [] + 2

Here the answer is similar and related to the 1st point, which is “2”. Because all concatenations or additions of arrays.

5. {} + []

This one will result in Zero (0), because the first element is an object and not an array (that converted to string).

6. {} + {}

This one will result in NaN.

P.S. Don’t use such question in job interviews :) They are nasty.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store