All numbers boolean-convert to true except for the following: <code>+/-0</code> and <code>NaN</code>

+

All numbers boolean-convert to true except for <code>+/-0</code> and <code>NaN</code>.

−

Boolean operators use type-conversion for the evaluation of their left hand side operands.

+

Boolean operators use type-conversion for the evaluation of their operand or operands.

<source lang="javascript">

<source lang="javascript">

1 && 0; // 0.

1 && 0; // 0.

Line 53:

Line 62:

</source>

</source>

−

=== Object to Primitive ===

+

=== Conversion to Primitive ===

−

Whenever the <code>+</code> 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:'''

+

Mathematical unary and binary operators, as well as boolean operators require the operands to be converted to primitives.

+

+

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

<source lang="javascript">

<source lang="javascript">

−

var o = {

+

var ob = {

valueOf : function() { return 1; }

valueOf : function() { return 1; }

};

};

−

o + 1; // 2.

+

ob + 1; // 2.

</source>

</source>

Otherwise, if <code>''o''.valueOf</code> results in an object &mdash;and <code>Object.prototype.valueOf</code> does &mdash; the object's <code>toString</code> is called.

Otherwise, if <code>''o''.valueOf</code> results in an object &mdash;and <code>Object.prototype.valueOf</code> does &mdash; the object's <code>toString</code> is called.

<source lang="javascript">

<source lang="javascript">

−

var o = { toString : function() { return "1"; } };

+

var ob = { toString : function() { return "1"; } };

−

o + 1; // "11".

+

ob + 1; // "11".

</source>

</source>

Line 73:

Line 84:

=== Converting to Number ===

=== Converting to Number ===

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

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

Converting to String

With the + operator, when either operand is a string, concatenation is performed.

All native objects have a toString method. Number.prototype.toString(base) is special in that it takes a base parameter.

15..toString(16)

String(15);// Calls ToPrimitive(input argument, hint String).

Conversion to Primitive

Mathematical unary and binary operators, as well as boolean operators require the operands to be converted to primitives.

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

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

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

parseInt(s, radix)

To force use of a particular base, use the radix parameter:

parseInt("09", base)// base from 2 to 36.

If radix is omitted, the base is determined by the contents of the string. Any string beginning with 0x or 0X represents a hexadecimal number. A string beginning with a leading 0 may, in older implementations, be parsed as octal (as if raxix were 8), in ECMA-262 Ed 3 (octal digits are 0-7). If string 09 is converted to 0.

var t ="0xf";Number(t);// 15+t;// 15

Primitive to Object with Property Accessors . and []

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