Performing Type Conversions
var cost:int = 2; cost += ' dollars';
var shipping = document.getElementById('shipping').value; total = quantity * price; tax /= 100; tax++; total *= tax; total += shipping;
Figure 4.12. Adding the string ‘5.00’ to the total has the impact of concatenation, converting the total number into an unusable string.
This issue doesn’t apply to other operators, though. For example, subtraction converts a string to a number and then performs the math, as the shopping example already demonstrated.
To perform math using strings, without worrying about creating bugs, you can forcibly convert the string to a number. There are many ways of doing so, starting with parseFloat() and parseInt(). These are “top-level” functions, which is to say they are not associated with any object and can be called directly. The first function always returns a floating-point number (aka, a decimal), and the latter, an integer. Both functions take the value to be converted as its first argument. The parseInt() function takes the radix as the second. The radix is the number’s base, as in base-8 (aka, octal), base-10 (decimal), and base-16 (hexadecimal). Although the second argument is optional, you should always provide it to be safe, and will normally use a value of 10:
total += parseFloat(shipping, 10);
To best use these functions, you should have an understanding of how they work. Both functions begin at the start of the string and extract a number until an invalid numeric character is encountered. If no valid number can be pulled from the start of the value, both functions return NaN (Figure 4.13):
parseInt('20', 10); parseInt('20.0', 10); parseInt('20 ducklings', 10); parseInt('I saw 20 ducklings.', 10);
Figure 4.13. How the parseInt() function extracts numbers from strings.
A trickier way to convert a string to a number is to prepend it with a +:
total += +shipping;
total += +(shipping);
Converting from a number to a string is far less likely to cause problems, but you can do so by invoking the toString() method:
var message = 'Your total is $' + total.toString();
The toString() method is supported by most objects and returns a string representation of the object itself.
Figure 4.14. How arithmetic is handled if undefined or null is involved.