11.1 Data Type Conversion
11.1.1 Object-to-Primitive Conversion
new Boolean(false) // Internal value is false, but object converts to true new Number(0) new String("") new Array( )
This leads to interesting results for arrays. Recall that the toString( ) method of arrays converts the array elements to strings, then returns the result of concatenating these strings, with commas in between. Therefore, an array with no elements converts to the empty string, which (as you can see in the table) converts to the number zero! Also, if an array has a single element that is a number n, the array converts to a string representation of n, which is then converted back to n itself. If an array contains more than one element, or if its one element is not a number, the array converts to NaN.
There is only one exception to this conversion rule: when a Date object is used with the + operator, conversion is performed with the toString( ) method. This exception exists because Date has both toString( ) and valueOf( ) methods. When a Date is used with +, you almost always want to perform a string concatenation. But when using a Date with the comparison operators, you almost always want to perform a numeric comparison to determine which of two times is earlier than the other.
Most objects either don't have valueOf( ) methods or don't have valueOf( ) methods that return useful results. When you use an object with the + operator, you usually get string concatenation rather than addition. When you use an object with a comparison operator, you usually get string comparison rather than numeric comparison.
An object that defines a custom valueOf( ) method may behave differently. If you define a valueOf( ) method that returns a number, you can use arithmetic and other operators with your object, but adding your object to a string may not behave as you expect: the toString( ) method is no longer called, and a string representation of the number returned by valueOf( ) is concatenated to the string.
Finally, remember that valueOf( ) is not called toNumber( ): strictly speaking, its job is to convert an object to a reasonable primitive value, so some objects may have valueOf( ) methods that return strings.
11.1.2 Explicit Type Conversions
There are a few other tricks that can be useful for performing explicit conversions. To convert a value to a string, concatenate it with the empty string:
var x_as_string = x + "";
To force a value to a number, subtract zero from it:
var x_as_number = x - 0;
And to force a value to boolean, use the ! operator twice:
var x_as_boolean = !!x;
11.1.3 Converting Numbers to Strings
var string_value = String(number); // Use the String( ) constructor as a function var string_value = number + ""; // Concatenate with the empty string
string_value = number.toString( );
The toString( ) method of the Number object (primitive numbers are converted to Number objects so that this method can be called) takes an optional argument that specifies a radix, or base, for the conversion. If you do not specify the argument, the conversion is done in base 10. But you can also convert numbers in other bases (between 2 and 36). For example:
var n = 17; binary_string = n.toString(2); // Evaluates to "10001" octal_string = "0" + n.toString(8); // Evaluates to "021" hex_string = "0x" + n.toString(16); // Evaluates to "0x11"
var n = 123456.789; n.toFixed(0); // "123457" n.toFixed(2); // "123456.79" n.toExponential(1); // "1.2e+5" n.toExponential(3); // "1.235e+5" n.toPrecision(4); // "1.235e+5" n.toPrecision(7); // "123456.8"
11.1.4 Converting Strings to Numbers
var number = Number(string_value); var number = string_value - 0;
The trouble with this sort of string-to-number conversion is that it is overly strict. It works only with base-10 numbers, and although it does allow leading and trailing spaces, it does not allow any non-space characters to appear in the string following the number.
To allow more flexible conversions, you can use parseInt( ) and parseFloat( ). These functions convert and return any number at the beginning of a string, ignoring any trailing non-numbers. parseInt( ) parses only integers, while parseFloat( ) parses both integers and floating-point numbers. If a string begins with "0x" or "0X", parseInt( ) interprets it as a hexadecimal number. For example:
parseInt("3 blind mice"); // Returns 3 parseFloat("3.14 meters"); // Returns 3.14 parseInt("12.34"); // Returns 12 parseInt("0xFF"); // Returns 255
parseInt("11", 2); // Returns 3 (1*2 + 1) parseInt("ff", 16); // Returns 255 (15*16 + 15) parseInt("zz", 36); // Returns 1295 (35*36 + 35) parseInt("077", 8); // Returns 63 (7*8 + 7) parseInt("077", 10); // Returns 77 (7*10 + 7)
parseInt("eleven"); // Returns NaN parseFloat("$72.47"); // Returns NaN