Type Conversions:JS in very flexible about the types of values it requres.Table below summarizes how values convert from one type to another in JS.Bold entries in the table highlight conversions that you may find surprising.Empty cells indicates that no conversions is necessary and none is performed.
1) Some numeric conversions may seem surprising:true converts to 1, and false and the empty string "" convert to 0.
2)Primitive-to-object conversions are straightforward:primitive values convert to their wrapper object as if by calling the String(),Number(),or Boolean() constructor.The exceptions are null and undefined:any attempt to use these values where an object is expected raises a TypeError exception rather than performing a conversion.
Convertions and Equality
The if statement converts undefined to false,but the == operator never attempts to convert its operands to booleans.
Explicit Convertions
When invoked without the new operator,however,they work as conversion functions and perform the conversions in the table above.
The Object() function does not throw a exception in this case:instead it simply returns a newly created empty object.
The toString() method defined by the Number class accepts an optional argument that specifies a radix,or base,for the conversion.
When working with financial or scientific data, you may want to convert numbers to strings in ways that you control over the number of decimal places or the number of significant digits in the output,or you may want to control whether exponential notations is used.the Number class defines 3 methods for these kinds of number-to-string conversions.
parseInt() and parseFloat() functions(these are global functions,not methods of any class) are more flexible.Both parseInt() and parseFloat() skip leading white-space,parse as many numeric characters as they can,and ignore anything that follows.If the first nonspace character is not part of a valid numeric literal,they return NaN.
parseInt() accepts an optional second argument specifying the radix(base) of the number to be parsed.Legal values are between 2 and 36.
Object to Primitive Conversions
Object-to-boolean conversions are trivial:all objects conver to true.This is so even for wrapper objects:new Boolean(false) is an object rather than a primitive value,and so it converts to true.
Object-to-string and object-to-number conversions are performed by invoking a method of the object to be converted.All objects inherit two conversioni methods.The first is called toString(),and its job is to return a string representation of the object.The default toString() method does not return a very interesting value(though we'll find it useful).The other object conversion function is called valueOf() which is less well-defined:it is supposed to convert an object to a primitive value that represents the object,if any such primitive value exists.
To convert an object to a string,JS takes these steps:
To convert an object to number,JS does the same thing,but it tries the valueOf() method first:
The details of this object-to-number conversion explain why an empty array converts to the number 0 and why an array with a single element may also convert to a number.
The + operator in JS performs numeric addition and string concatenation.
if either of its operands is an object, JS converts the object using a special object-to-primitive conversion rather than object-to-number conversion.The == operator is similar.