4

I get these outputs when the following operations are carried out

var t = {} + {}; // Alerts [object Object][object Object]
t = {} + 1 //Alerts [object Object]1
t = {} + "hello" //Alerts [object Object]hello
t = {} + function(){} //Alerts [object Object]function(){}
t = {} + [] //Alerts [object Object]

In the last case alone it alerts [Object object] shouldn't it display [object Object][object Object] for this too?

Tested in Firefox12.0.

1 Answer 1

7

No, because the second part of the output is the stringified form of the empty array []. Arrays are stringified as a comma-separated list of stringified values, so the empty array stringifies to the empty string. You can confirm this with console.log([] + "" === "").

Therefore, {} + [] results in the equivalent "[object Object]" + "".

Sign up to request clarification or add additional context in comments.

1 Comment

Just to add that when evaluating the addition operator +, each expression is converted to a primitive by calling its toString method (if it has one).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.