Converting to String

When either operand is a string, concatenation is performed.

Object to Primitive

Whenever the + operator is used, the operands must be converted into primitive values. First, the interpreter calls the object's valueOf to get a primitive value. If the result is a primitive value, then that value is used. Example:

var o ={
valueOf :function(){return1;}};
o +1;// 2.

Otherwise, if o.valueOf results in an object —and Object.prototype.valueOf does — the object's toString is called.

var o ={ toString :function(){return"1";}};
o +1;// "11".

Converting to Number

Converting strings is a very common requirement and many methods can be used. Any mathematical operator except the concatenation/addition operator will force type-conversion to number.

Primitive to Object

Property access operation on string, number, and boolean primitives results in the creation of a temporary object.