parseInt()
The parseInt() function ignores leading white space. The parseInt() function checks the string character by character.
If this first non-white space character isn't a number, the minus sign, or the plus sign, parseInt() returns NaN. The parseInt() function returns NaN for empty string, while Number() returns 0.
If the first character is a number, plus, or minus, then the checking continues until the end of the string is reached or a nonnumeric character is found. For instance, "1234asdf" is converted to 1234 because "asdf" is ignored.
<!DOCTYPE html>
<html>
<head>
<title>Number Example</title>
<script type="text/javascript">
document.writeln(parseInt("1234asdf"));
</script>
</head>
<body>
</body>
</html>
"1.2" is converted to 1 because the decimal point is not a valid integer character.
<!DOCTYPE html>
<html>
<head>
<title>Number Example</title>
<script type="text/javascript">
document.writeln(parseInt("1.2"));
</script>
</head>
<body>
</body>
</html>
The parseInt() function recognizes the decimal, octal, and hexadecimal formats.
<!DOCTYPE html>
<html>
<head>
<title>Number Example</title>
<script type="text/javascript">
document.writeln(parseInt("0xA"));
</script>
</head>
<body>
</body>
</html>
"" is converted to NaN.
<!DOCTYPE html>
<html>
<head>
<title>Number Example</title>
<script type="text/javascript">
document.writeln(parseInt(""));
</script>
</head>
<body>
</body>
</html>
If the string begins with "0" followed by a 0-7 number, it is interpreted as an octal value.
<!DOCTYPE html>
<html>
<head>
<title>Number Example</title>
<script type="text/javascript">
document.writeln(parseInt("0123"));
</script>
</head>
<body>
</body>
</html>
radix
parseInt() provides a second argument: the radix.
For hexadecimal format, the radix is 16.
<!DOCTYPE html>
<html>
<head>
<title>Number Example</title>
<script type="text/javascript">
document.writeln(parseInt("0xAE", 16));
</script>
</head>
<body>
</body>
</html>
The leading "0x" is not necessary by providing the hexadecimal radix:
<!DOCTYPE html>
<html>
<head>
<title>Number Example</title>
<script type="text/javascript">
document.writeln(parseInt("AE", 16));
</script>
</head>
<body>
</body>
</html>
Passing in a radix can change the outcome of the conversion.
<!DOCTYPE html>
<html>
<head>
<title>Number Example</title>
<script type="text/javascript">
document.writeln(parseInt("10", 2)); //2 - parsed as binary
document.writeln(parseInt("10", 8)); //8 - parsed as octal
document.writeln(parseInt("10", 10));//10 - parsed as decimal
document.writeln(parseInt("10", 16));//16 - parsed as hexadecimal
</script>
</head>
<body>
</body>
</html>
JavaScript Book
Language Basics
- JavaScript Data Types
- typeof Operator
- The Undefined Type
- null Type
- null vs undefined
- Boolean Type
- Boolean() casting function
- The Literials of Number Type
- Octal Integer
- Hexadecimal
- Floating-Point Values
- Value range
- NaN
- Number Conversions:Number(), parseInt() and parseFloat()
- Number() function
- parseInt()
- parseFloat()
- The String Type
- String Literals and Escapes
- Get the String Length
- Converting to a String with toString() method
- Convert Number to String with radix
- Convert to String with String() casting function