0

Can anyone explain me that what happens when I add a variable of type object with a numeric value?

I did the following in chrome dev console :

> //Denotes input in dev console
< //Denotes output


> var a={};
< undefined
> a
> Object {}
> a=a+1;
< "[object Object]1"
> a
< "[object Object]1"
> b=2
< 2
> a+b
< "[object Object]12"

Also note that within bracket First is lower-case object and 2nd is uppercase Object. Thanks.

1
  • I don't know for what reason someone downvoted this question. Commented Jan 2, 2016 at 7:09

5 Answers 5

1

Rules for JavaScript and mixed-type operations are quite convoluted and depend on the operation, for example for + an extremely simplified description is:

  • If both are number then the arithmetic sum is performed
  • If any of the two is not a number then both are converted to string and concatenation is performed

The string conversion of an object (unless you defined what to do) is just "[object Object]" and that explains the results you described in the question.

Note that this is not the whole truth... things are much more convoluted and a full description require concepts like .toValue members, special cases for Date objects and so on. Moreover things are getting more and more complex as time passes (ES6 behavior for example requires introduction of concepts like distinction between "exoticToPrim" and "OrdinaryToPrimitive").

Unfortunately I am not kidding.

In case you want to understand better pay attention when writing test programs that you cannot trust the == operator to check what the result is because the rules for that equality operator are even more convoluted (bordering total madness, actually). == in Javascript is just a bug-hiding shelter and should have no place in any code (if strangely enough you really want what it does then implement that behavior explicitly and comment on why it is needed instead of just using ==).

Always use ===.

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

Comments

1
a = a+1
  1. It first looks if a is a numerical value or not, if it is then the behaviour is normal mathematical operation.
  2. Otherwise it typecast a in a string and proceed with normal string concatenation. The default conversion from an object to string is [object Object], so a+1 will be [object Object]1

Try doing stringify({});, it prints [object Object]

Comments

1

When you use plus operator object a converts to string using toString method. And then strings a and b concatenate

Comments

1

This is called as type coercion. variable a cannot be added with 1 technically. So it coerced to string and then get concatenated with the numeric 1.

var a = {} + 1; //[object object]1
//--> string form of {} will be [object object]
//--> + operator will use its overload concatenation when it is used over a string.
//--> The type of value returned after this operation will be a string.

Comments

1

You will love this one: https://youtu.be/20BySC_6HyY?t=1m26s It's all about JavaScript trying to correct your variable types. It has some crazy edge cases. e.g.

"+" is to "add" 2 strings to form new one or to add 2 numbers to make a "sum". What is in your situation - you are trying to change variable types to strings or numbers and make new string or number. Js decided to make new string:

var a = {}
// Output: Object {}
a.toString();
// Output: "[object Object]"

To learn more i highly recommend this:

http://jibbering.com/faq/notes/type-conversion/

1 Comment

Thanks for the links.I highly appreciate it.

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.