ToNumber (Operator/internal)

An internal operator for converting values.

Availability:

ECMAScript edition - 2

This internal operator converts its argument to an appropriate numeric value.

The ToNumber operator converts its input values according to the following table:

Input TypeResult
UndefinedReturns NaN
Null0
Boolean1 if true, 0 if false.
NumberNo conversion, the input value is returned unchanged.
StringThe value of a sequence of characters that can reasonably be converted to a number, and if not then NaN is returned.
ObjectInternally, a conversion to one of the primitive types happens followed by a conversion from that type to a number. Some objects will return a number that is readily usable. Others will return something that cannot be converted and NaN will result.

The string scanning algorithm copes with spelled out special values such as Infinity, exponential values and can scan integers in Octal and Hexadecimal notation as well as decimal.

See also:Cast operator, Conversion, Implicit conversion, isFinite(), isNaN(), Number, Type conversion

Cross-references:

ECMA 262 edition 2 - section - 9.3

ECMA 262 edition 3 - section - 9.3