When String() is called as a function rather than a constructor, it performs a type conversion.
The internal ToString conversion facilities are used for type conversion with the additional handling of a missing argument provided by the constructor itself.
Value | Result |
No argument | An empty string " " |
undefined | "undefined" |
null | "null" |
Boolean | If the argument is true, then the result is "true" otherwise the result is "false" |
Number | Special cases are provided for NaN and Infinity where "NaN" and "Infinity" will be returned. Otherwise the string is a textual representation of the value. |
String | No conversion, the input value is returned unchanged. |
Object | An internal conversion to a primitive takes place followed by a conversion from that primitive to a string. Some objects will return a string value that is immediately useful. |
The result of calling this function is a string version of the value passed in. If there is no value passed in argument an empty string is returned.
Converting numbers to strings can yield some strange effects due to rounding errors. Taking a numeric value and simply converting it is fairly reliable. However, the result of a numeric expression, being cast to a string directly rather than to a number variable and thence to a string, seems to expose some weaknesses in the arithmetic in some implementations.
ECMA 262 edition 2 - section - 15.1.3.4
ECMA 262 edition 2 - section - 15.5.1
ECMA 262 edition 3 - section - 15.5.1
Wrox Instant JavaScript - page 36
Prev | Home | Next |
String() constructor | Up | String.anchor() |
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. |