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>
  
Click to view the demo

"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>
  
Click to view the demo

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>
  
Click to view the demo

"" is converted to NaN.

 
<!DOCTYPE html>
<html>
<head>
    <title>Number Example</title>
    <script type="text/javascript">
        document.writeln(parseInt(""));
        
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

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>
  
Click to view the demo

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>
  
Click to view the demo

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>
  
Click to view the demo

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>
  
Click to view the demo
Home 
  JavaScript Book 
    Language Basics  

Data Types:
  1. JavaScript Data Types
  2. typeof Operator
  3. The Undefined Type
  4. null Type
  5. null vs undefined
  6. Boolean Type
  7. Boolean() casting function
  8. The Literials of Number Type
  9. Octal Integer
  10. Hexadecimal
  11. Floating-Point Values
  12. Value range
  13. NaN
  14. Number Conversions:Number(), parseInt() and parseFloat()
  15. Number() function
  16. parseInt()
  17. parseFloat()
  18. The String Type
  19. String Literals and Escapes
  20. Get the String Length
  21. Converting to a String with toString() method
  22. Convert Number to String with radix
  23. Convert to String with String() casting function