How to convert a string of any base to an integer in JavaScript?

An integer can be represented in various formats in programming languages such as Binary (base 2), Decimal (base 10), Hexadecimal (base 16), and Octal (base 8). A Binary number consists of two digits only (0 & 1), whereas a decimal consists of digits from 0 to 9.

We can convert a string to an integer using the parseInt() method. When the integer is represented in a different base, we need to pass the base parameter to decode it correctly.

Syntax

parseInt(string_value, base)
parseInt(string_value)  // Defaults to base 10

Parameters

  • string_value: The string to convert to an integer
  • base (optional): The base of the number system (2-36). Defaults to 10

Return Value

Returns an integer parsed from the string, or NaN if the string cannot be parsed.

Example 1: Converting Without Base Parameter

When no base is specified, parseInt() defaults to base 10 (decimal):

<!DOCTYPE html>
<html>
<head>
    <title>String to Integer Conversion</title>
</head>
<body>
    <h2 style="color:green">Welcome To Tutorials Point</h2>
    <script>
        let stringConversion = (string_value) => {
            console.log("Initial Type: " + typeof string_value);
            let integer_value = parseInt(string_value);
            console.log("Final Type: " + typeof integer_value);
            console.log("Result: " + integer_value);
            console.log("---");
        };
        
        stringConversion("8475628");
        stringConversion("101010");
        stringConversion("0x3278");  // Hexadecimal prefix
    </script>
</body>
</html>
Initial Type: string
Final Type: number
Result: 8475628
---
Initial Type: string
Final Type: number
Result: 101010
---
Initial Type: string
Final Type: number
Result: 12920

Example 2: Converting With Different Base Values

Here we specify different bases to convert strings from various number systems:

<!DOCTYPE html>
<html>
<head>
    <title>Base Conversion Example</title>
</head>
<body>
    <h2 style="color:green">Welcome To Tutorials Point</h2>
    <script>
        let baseConversion = (string_value, base) => {
            console.log("String: " + string_value + ", Base: " + base);
            let integer_value = parseInt(string_value, base);
            console.log("Integer value: " + integer_value);
            console.log("---");
        };
        
        baseConversion("101010", 2);    // Binary to decimal
        baseConversion("101", 10);      // Decimal to decimal
        baseConversion("256472", 8);    // Octal to decimal
        baseConversion("FF", 16);       // Hexadecimal to decimal
    </script>
</body>
</html>
String: 101010, Base: 2
Integer value: 42
---
String: 101, Base: 10
Integer value: 101
---
String: 256472, Base: 8
Integer value: 89914
---
String: FF, Base: 16
Integer value: 255

Common Base Systems

Base System Digits Used Example
2 Binary 0, 1 "1010" ? 10
8 Octal 0-7 "12" ? 10
10 Decimal 0-9 "10" ? 10
16 Hexadecimal 0-9, A-F "A" ? 10

Key Points

  • parseInt() stops parsing when it encounters an invalid character
  • Base parameter must be between 2 and 36
  • Returns NaN if the string cannot be parsed
  • Leading whitespace is ignored

Conclusion

Use parseInt(string, base) to convert strings from any number system to decimal integers. The base parameter is essential when working with non-decimal number systems like binary, octal, or hexadecimal.

Updated on: 2026-03-15T23:19:00+05:30

564 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements