2

What will be the output of the of following code:

console.log({}.valueOf()+15);//Output:[object Object]15

Will the return value from the .valueOf() method be string in the above code or is simply an object since .toString() will result in same but with this method returned value will be string anyhow.I just wanted to know what will be the type of the value the .valueOf() returns in above code.If return value is a string ,for what other object will valueOf() method returns string value except for [new String("abc")]

asked Apr 21, 2013 at 17:11
1
  • Who wants to close this post???? Commented Apr 21, 2013 at 17:50

3 Answers 3

3

The .valueOf() of a plain object will be the same object. (This is NOT the same as its .toString() method.)

var o = {};
typeof o.valueOf(); // "object"
o === o.valueOf(); // true

The + operator sees that you're not using a number for the first operand, so instead of addition, it does string concatenation and calls the .toString() value of both of its operands.

That's why you get [object Object]15.


You can test this by changing the toString() output.

var o = {};
o.toString = function() { return "foobar" }
o.toString(); // "foobar"
o + 15; // "foobar15"
o.valueOf() + 15; // "foobar15"
answered Apr 21, 2013 at 17:18
Sign up to request clarification or add additional context in comments.

6 Comments

the valueOf() method returns plain object which is a string
@Maizere: No, the .valueOf() object returns a plain object which is not a string. It's just the same object. The + operator returns a string for reasons that I explained in my answer below the first code example.
plz see the comment in the Klink post above
@Maizere: Kolink's answer and comment is wrong. The .valueOf() is not the same as the .toString(), and does not return a string.
@scram This o === o.valueOf(); may not always be true. Implementation specific according to ECMA-262.
|
3

valueOf returns a primitive value. Exactly what type of primitive value depends on the object.

To demonstrate:

typeof Number(0).valueOf(); // number
typeof Boolean(0).valueOf(); // boolean

The point is that, whatever it returns, it is a primitive value. toString, on the other hand, always returns a string.

In the case of {}, there is no valueOf method other than the root Object.prototype.valueOf, which is identical to Object.prototype.toString, returning a string of the form [object Constructor]. Which being a string results in concatenation with 15.

answered Apr 21, 2013 at 17:16

7 Comments

but i m adding with a number
will number also gets coerced to String
Yes, because the first operand to + is a string in this case. If it were a number, you would be adding it.
Now i have started to know about type casting in javascript
last question for what other object will valueOf() method returns string
|
0

To get the type of an object in JS, you can use the typeof function:

typeof({}) // outputs 'object'
typeof({}.valueOf()) // outputs 'object'
typeof({}.valueOf()+15) // outputs 'string'

This indicates that the value coercion is taking place when trying to "add" 15, which instead is interpreted as string concatenation.

answered Apr 21, 2013 at 17:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.