This internal operator converts its argument to an appropriate numeric value.
The ToNumber operator converts its input values according to the following table:
| Input Type | Result |
|---|---|
| Undefined | Returns NaN |
| Null | 0 |
| Boolean | 1 if true, 0 if false. |
| Number | No conversion, the input value is returned unchanged. |
| String | The value of a sequence of characters that can reasonably be converted to a number, and if not then NaN is returned. |
| Object | Internally, 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 |
| Prev | Home | Next |
| Token | Up | ToObject |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. | ||