There are JavaScript functions (not string methods) that convert strings to numbers
|
parseInt("15") or equivalently parseInt("15",10) both return the number 15 in base 10
|
The optional second argument is the desired radix so that parseInt("15",8) returns 17 in octal
|
If input string begins with "0x" the default radix is 16 (hexadecimal) whereas if it begins with "0" the default radix is 8 (octal) Ñ otherwise, the default radix is 10
|
x = 1 + "1"; // evaluates to "11"
|
x = 1 + parseInt("1"); // evaluates to 2
|
parseFloat(string) returns a floating point value
-
var x = "0.0314E+2"; var y = parseFloat(x); // sets y=3.14
|
On platforms that support it, parseInt and parseFloat return NaN (Not a Number) if parsing fails
|