๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ parseInt
parseInt() - JavaScript | MDN
If parseInt encounters a character ... and returns the integer value parsed up to that point. For example, parseInt("2", 2) returns NaN because 2 is not a valid numeral in the binary number system....
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_parseint.asp
JavaScript parseInt() Method
Older browsers will return 8 for parseInt("010"). Older versions of ECMAScript used octal (radix 8) for values beginning with "0".
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ java โ€บ java integer parsing
Java Integer Parsing
September 1, 2008 - public class Test { public static void main(String args[]) { int x =Integer.parseInt("9"); double c = Double.parseDouble("5"); int b = Integer.parseInt("444",16); System.out.println(x); System.out.println(c); System.out.println(b); } }
๐ŸŒ
KeyCDN
keycdn.com โ€บ support โ€บ parseint
parseInt() - Explained with Examples - KeyCDN Support
For this, we've outlined three octal examples, three decimal examples, and three hexadecimal examples. parseInt('10', 8); //returns 8 parseInt('010') //returns 10 parseInt('17', 8); //returns 15
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ parseint-in-javascript-js-string-to-int-example
parseInt() in JavaScript โ€“ JS String to Int Example
February 11, 2022 - With the parseInt function, we can can extract 3 from the string and turn it to an actual number. Here is an example:
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-number-parseint-method
JavaScript Number parseInt() Method - GeeksforGeeks
July 25, 2018 - // Basic string to integer conversion console.log(parseInt("123")); // Stops at non-numeric character console.log(parseInt("45px")); // Using radix parameter (base 2) console.log(parseInt("1010", 2)); // Invalid conversion console.log(parse...
๐ŸŒ
H2K Infosys
h2kinfosys.com โ€บ blog โ€บ introduction to parseint in java
Introduction to ParseInt in Java
September 24, 2024 - The parseInt function of the Integer class receives the string โ€œ123โ€ as an input in this example, and it transforms the string into the integer value 123 and stores it in the number variable.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ java-integer-parseint-method
Java Integer parseInt() Method - Javatpoint
Java Integer parseInt() Method - The Java parseInt() method is a method of Integer class that belong to java.lang package. The parseInt() method in Java is crucial for converting string representations of numbers into actual integer values. This capability is fundamental in various programming ...
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref_parseint.asp
JavaScript parseInt() Function
Note: Older browsers will result parseInt("010") as 8, because older versions of ECMAScript, (older than ECMAScript 5, uses the octal radix (8) as default when the string begins with "0".
Find elsewhere
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ parseint() in javascript
ParseInt() in JavaScript - Scaler Topics
January 1, 2024 - We can use parseInt() in Javascript in two ways, one without the radix value (base of the integer value) and the other by passing a radix value as the second argument.
๐ŸŒ
Tabnine
tabnine.com โ€บ home โ€บ how to use parseint() in javascript
How to Use parseInt() in JavaScript - Tabnine
July 25, 2024 - When parseInt() reaches a character that is not numeral in the specified radix, it stops reading the string and returns the integer value parsed up to that point. For radices above 10, letters of the English alphabet indicate numerals greater than 9. For example, when using base 16 (hexadecimal numbers) โ€˜Aโ€™ through โ€˜Fโ€™ are used (A = 10, F = 15).
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ integer-valueof-vs-integer-parseint-with-examples
Integer.valueOf() vs Integer.parseInt() with Examples - GeeksforGeeks
July 11, 2025 - public static int parseInt(String s, int radix) throws NumberFormatException ยท Example: Java ยท
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Number โ€บ parseInt
Number.parseInt() - JavaScript | MDN
function roughScale(x, base) { const parsed = Number.parseInt(x, base); if (Number.isNaN(parsed)) { return 0; } return parsed * 100; } console.log(roughScale(" 0xF", 16)); // Expected output: 1500 console.log(roughScale("321", 2)); // Expected output: 0
๐ŸŒ
TechOnTheNet
techonthenet.com โ€บ js โ€บ number_parseint.php
JavaScript: Number parseInt() method
The second output to the console log returned 123 since the parseInt() method will parse the string value '123ABC4' until a non-numeric character is encountered and then it will discard the remainder of the string, returning the integer value. The third output to the console log returned NaN since the string value 'ABC123' does not start with a numeric value. You can also specify a radix parameter to evaluate your string_value in a different mathematical numeral system than the Decimal system. For example, let's evaluate our numbers as Binary:
๐ŸŒ
Global Tech Council
globaltechcouncil.org โ€บ home โ€บ what is parseint in java?
What is parseInt in Java? - Global Tech Council
August 22, 2025 - So, parseInt(โ€œ101โ€, 2) would return the integer 5, because 101 in binary is 5 in decimal. This method is a bit more advanced. It lets you convert a part of a string into an integer. The CharSequence is the type of the string you are working with, and you can specify where in the string to start (beginIndex) and where to end (endIndex). The radix works the same way as in the previous method, letting you define the number system. For example, if you have a string like โ€œabc123defโ€ and you only want to convert โ€œ123โ€, you would call parseInt(โ€œabc123defโ€, 3, 6, 10) ...
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ library โ€บ built-in โ€บ parseInt
JavaScript parseInt()
// parse the string to decimal let intDate = parseInt(stringDate, 10); console.log(intDate) // Output: 1996
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ can someone explain mdn's parseint function example?
r/learnjavascript on Reddit: Can someone explain MDN's parseInt function example?
June 20, 2022 -

function roughScale(x, base) {

const parsed = Number.parseInt(x, base);

if (Number.isNaN(parsed)) { return 0; }

return parsed * 100;

}

console.log(roughScale(' 0xF', 16)); // expected output: 1500

console.log(roughScale('321', 2)); // expected output: 0

I understand that parseInt is a method used to parse a string and return an integer. But I'm kind of confused how the function gets the expected output results.

Wouldn't the returned parsed values be:

(0, 16) * 100?

and

(321,16) * 100?

So why are the expected output 1500 and 0?

Also what does 'Number.NaN(parsed)' mean does it mean if the string parsed is not a number it would return 0?

Top answer
1 of 8
47

Usually this is done like this:

  • init result with 0
  • for each character in string do this
    • result = result * 10
    • get the digit from the character ('0' is 48 ASCII (or 0x30), so just subtract that from the character ASCII code to get the digit)
    • add the digit to the result
  • return result

Edit: This works for any base if you replace 10 with the correct base and adjust the obtaining of the digit from the corresponding character (should work as is for bases lower than 10, but would need a little adjusting for higher bases - like hexadecimal - since letters are separated from numbers by 7 characters).

Edit 2: Char to digit value conversion: characters '0' to '9' have ASCII values 48 to 57 (0x30 to 0x39 in hexa), so in order to convert a character to its digit value a simple subtraction is needed. Usually it's done like this (where ord is the function that gives the ASCII code of the character):

Copydigit = ord(char) - ord('0')

For higher number bases the letters are used as 'digits' (A-F in hexa), but letters start from 65 (0x41 hexa) which means there's a gap that we have to account for:

Copydigit = ord(char) - ord('0')
if digit > 9 then digit -= 7

Example: 'B' is 66, so ord('B') - ord('0') = 18. Since 18 is larger than 9 we subtract 7 and the end result will be 11 - the value of the 'digit' B.

One more thing to note here - this works only for uppercase letters, so the number must be first converted to uppercase.

2 of 8
28

The source code of the Java API is freely available. Here's the parseInt() method. It's rather long because it has to handle a lot of exceptional and corner cases.

Copypublic static int parseInt(String s, int radix) throws NumberFormatException {
    if (s == null) {
        throw new NumberFormatException("null");
    }

    if (radix < Character.MIN_RADIX) {
        throw new NumberFormatException("radix " + radix +
            " less than Character.MIN_RADIX");
    }

    if (radix > Character.MAX_RADIX) {
        throw new NumberFormatException("radix " + radix +
            " greater than Character.MAX_RADIX");
    }

    int result = 0;
    boolean negative = false;
    int i = 0, max = s.length();
    int limit;
    int multmin;
    int digit;

    if (max > 0) {
        if (s.charAt(0) == '-') {
            negative = true;
            limit = Integer.MIN_VALUE;
            i++;
        } else {
            limit = -Integer.MAX_VALUE;
        }
        multmin = limit / radix;
        if (i < max) {
            digit = Character.digit(s.charAt(i++), radix);
            if (digit < 0) {
                throw NumberFormatException.forInputString(s);
            } else {
                result = -digit;
            }
        }
        while (i < max) {
            // Accumulating negatively avoids surprises near MAX_VALUE
            digit = Character.digit(s.charAt(i++), radix);
            if (digit < 0) {
                throw NumberFormatException.forInputString(s);
            }
            if (result < multmin) {
                throw NumberFormatException.forInputString(s);
            }
            result *= radix;
            if (result < limit + digit) {
                throw NumberFormatException.forInputString(s);
            }
            result -= digit;
        }
    } else {
        throw NumberFormatException.forInputString(s);
    }
    if (negative) {
        if (i > 1) {
            return result;
        } else { /* Only got "-" */
            throw NumberFormatException.forInputString(s);
        }
    } else {
        return -result;
    }
}
๐ŸŒ
vteams
vteams.com โ€บ blog โ€บ use-of-parseint-java-programming
Learn the Basics & Ins-And-Outs of PARSEint Java Programming
February 23, 2024 - Then the method will run and return you the desired results, provided everything went well. Here is a sample example. The above-mentioned, integer.parseInt Java example is passing โ€˜123โ€™ as a string and not a number into the method, which is being called in the second line from its integer class.