Values are continuously being changed from one type of value to another inside a JavaScript interpreter. The very nature of its weak data typing and the automatic promotion and demotion of values in expressions causes implicit changes in the type of values at almost every step of a script's execution.
The conversion behavior of each type is discussed in the coverage of each of the Native primitive types that are built into the interpreter.
Refer to the Cast operator description as well for further information.
There are also some internal conversion operators that provide the basic underlying conversion facilities. These are described in the following topics:
ToBoolean
ToInt32
ToInteger
ToNumber
ToObject
ToPrimitive
ToString
toString()
ToUint16
ToUint32
These are given individual topics on account of their description in the ECMA standard. There are however four basic and fundamental conversions. These are:
Boolean
Number
String
Object
The internal function topics cover what happens inside an interpreter. In this topic and the several following, we are concerned with the script visible effects of conversion.
There are some circumstances where the conversion of an object to a number or a string can be ambiguous. The cases of concatenate or add and the relative expression operators are such an example. The Date objects will prefer to be converted to a string rather than a number if at all possible and consistent with the context.
Many objects have toString() methods. Not as many will support the valueOf() method. The valueOf() method is so named because it is not implicitly a toNumber() method. It may return a string because that is the most reasonable primitive. It is really a toPrimitive() method.
See also: | Argument, Cast operator, Integer constant, Integer promotion, LValue, ToBoolean, ToInt32, ToInteger, ToNumber, ToObject, ToPrimitive, ToString, toString(), ToUint16, ToUint32 |
Prev | Home | Next |
Control character | Up | Conversion to a Boolean |
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. |