Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

parseInt()

BaselineWidely available

TheparseInt() function parses a string argument and returns an integer of the specifiedradix (the base in mathematical numeral systems).

Try it

console.log(parseInt("123"));// 123 (default base-10)console.log(parseInt("123", 10));// 123 (explicitly specify base-10)console.log(parseInt("   123 "));// 123 (whitespace is ignored)console.log(parseInt("077"));// 77 (leading zeros are ignored)console.log(parseInt("1.9"));// 1 (decimal part is truncated)console.log(parseInt("ff", 16));// 255 (lower-case hexadecimal)console.log(parseInt("0xFF", 16));// 255 (upper-case hexadecimal with "0x" prefix)console.log(parseInt("xyz"));// NaN (input can't be converted to an integer)

Syntax

js
parseInt(string)parseInt(string, radix)

Parameters

string

A string starting with an integer. Leadingwhitespace in this argument is ignored.

radixOptional

An integer between2 and36 that represents theradix (the base in mathematical numeral systems) of thestring. It is converted to a32-bit integer; if it's nonzero and outside the range of [2, 36] after conversion, the function will always returnNaN. If0 or not provided, the radix will be inferred based onstring's value. Be careful — this doesnot always default to10! Thedescription below explains in more detail what happens whenradix is not provided.

Return value

An integer parsed from the givenstring, orNaN when

  • theradix as a 32-bit integer is smaller than2 or bigger than36, or
  • the first non-whitespace character cannot be converted to a number.

Note:JavaScript does not have the distinction of "floating point numbers" and "integers" on the language level.parseInt() andparseFloat() only differ in their parsing behavior, but not necessarily their return values. For example,parseInt("42") andparseFloat("42") would return the same value: aNumber 42.

Description

TheparseInt functionconverts its first argument to a string, parses that string, then returns an integer orNaN.

If notNaN, the return value will be the integer that is the first argument taken as a number in the specifiedradix. (For example, aradix of10 converts from a decimal number,8 converts from octal,16 from hexadecimal, and so on.)

Theradix argument isconverted to a number. If it's unprovided, or if the value becomes 0,NaN orInfinity (undefined is coerced toNaN), JavaScript assumes the following:

  1. If the inputstring, with leading whitespace and possible+/- signs removed, begins with0x or0X (a zero, followed by lowercase or uppercase X),radix is assumed to be16 and the rest of the string is parsed as a hexadecimal number.
  2. If the inputstring begins with any other value, the radix is10 (decimal).

Note:Other prefixes like0b, which are valid innumber literals, are treated as normal digits byparseInt().parseInt() doesnot treat strings beginning with a0 character as octal values either. The only prefix thatparseInt() recognizes is0x or0X for hexadecimal values — everything else is parsed as a decimal value ifradix is missing.Number() orBigInt() can be used instead to parse these prefixes.

If the radix is16,parseInt() allows the string to be optionally prefixed by0x or0X after the optional sign character (+/-).

If the radix value (coerced if necessary) is not in range [2, 36] (inclusive)parseInt returnsNaN.

For radices above10, letters of the English alphabet indicate numerals greater than9. For example, for hexadecimal numbers (base16),A throughF are used. The letters are case-insensitive.

parseInt understands exactly two signs:+ for positive, and- for negative. It is done as an initial step in the parsing after whitespace is removed. If no signs are found, the algorithm moves to the following step; otherwise, it removes the sign and runs the number-parsing on the rest of the string.

IfparseInt encounters a character in the input string that is not a valid numeral in the specifiedradix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. For example,parseInt("2", 2) returnsNaN because2 is not a valid numeral in the binary number system. Likewise, although1e3 technically encodes an integer (and will be correctly parsed to the integer1000 byparseFloat()),parseInt("1e3", 10) returns1, becausee is not a valid numeral in base 10. Because. is not a numeral either, the return value will always be an integer.

If the first character cannot be converted to a number with the radix in use,parseInt returnsNaN. Leading whitespace is allowed.

For arithmetic purposes, theNaN value is not a number in any radix. You can call theNumber.isNaN function to determine if the result ofparseInt isNaN. IfNaN is passed on to arithmetic operations, the operation result will also beNaN.

Because large numbers use thee character in their string representation (e.g.,6.022e23 for 6.022 × 1023), usingparseInt to truncate numbers will produce unexpected results when used on very large or very small numbers.parseInt shouldnot be used as a substitute forMath.trunc().

To convert a number to its string literal in a particular radix, usethatNumber.toString(radix).

BecauseparseInt() returns a number, it may suffer from loss of precision if the integer represented by the string isoutside the safe range. TheBigInt() function supports parsing integers of arbitrary length accurately, by returning aBigInt.

Examples

Using parseInt()

The following examples all return15:

js
parseInt("0xF", 16);parseInt("F", 16);parseInt("17", 8);parseInt("015", 10);parseInt("15,123", 10);parseInt("FXX123", 16);parseInt("1111", 2);parseInt("15 * 3", 10);parseInt("15e2", 10);parseInt("15px", 10);parseInt("12", 13);

The following examples all returnNaN:

js
parseInt("Hello", 8); // Not a number at allparseInt("546", 2); // Digits other than 0 or 1 are invalid for binary radix

The following examples all return-15:

js
parseInt("-F", 16);parseInt("-0F", 16);parseInt("-0XF", 16);parseInt("-17", 8);parseInt("-15", 10);parseInt("-1111", 2);parseInt("-15e1", 10);parseInt("-12", 13);

The following example returns224:

js
parseInt("0e0", 16);

parseInt() does not handleBigInt values. It stops at then character, and treats the preceding string as a normal integer, with possible loss of precision.

js
parseInt("900719925474099267n");// 900719925474099300

You should pass the string to theBigInt() function instead, without the trailingn character.

js
BigInt("900719925474099267");// 900719925474099267n

parseInt doesn't work withnumeric separators:

js
parseInt("123_456"); // 123

Using parseInt() on non-strings

parseInt() can have interesting results when working on non-strings combined with a high radix; for example,36 (which makes all alphanumeric characters valid numeric digits).

js
parseInt(null, 36); // 1112745: The string "null" is 1112745 in base 36parseInt(undefined, 36); // 86464843759093: The string "undefined" is 86464843759093 in base 36

In general, it's a bad idea to useparseInt() on non-strings, especially to use it as a substitution forMath.trunc(). It may work on small numbers:

js
parseInt(15.99, 10); // 15parseInt(-15.1, 10); // -15

However, it only happens to work because the string representation of these numbers uses basic fractional notation ("15.99","-15.1"), whereparseInt() stops at the decimal point. Numbers greater than or equal to 1e+21 or less than or equal to 1e-7 use exponential notation ("1.5e+22","1.51e-8") in their string representation, andparseInt() will stop at thee character or decimal point, which always comes after the first digit. This means for large and small numbers,parseInt() will return a one-digit integer:

js
parseInt(4.7 * 1e22, 10); // Very large number becomes 4parseInt(0.00000000000434, 10); // Very small number becomes 4parseInt(0.0000001, 10); // 1parseInt(0.000000123, 10); // 1parseInt(1e-7, 10); // 1parseInt(1000000000000000000000, 10); // 1parseInt(123000000000000000000000, 10); // 1parseInt(1e21, 10); // 1

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-parseint-string-radix

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp