[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/parseInt [Back]  [Original]

parseInt() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

parseInt()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..

parseInt() .

In this article

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)

parseInt(string, radix);

string

, . string , ( ToString). .

radix

2 36, string, . 10. , . , .

, ( ) . , NaN.

parseInt , NaN. ( NaN) (string), (radix). , 10 , 8 - , 16 - . 10, 9 . , ( 16) A F.

parseInt , , (, ) , , . parseInt . .

e (, 6.022e23), parseInt , . parseInt Math.floor().

undefined ( ) 0 ( ), JavaScript :

  • string "0x" "0X", 16, .
  • string "0", 8, 10, . ECMAScript 5 10 ( ), , parseInt.
  • string , ( 10).

, parseInt NaN.

, NaN - . , parseInt NaN , isNaN. NaN , NaN.

, intValue.toString(radix).

: parseInt

15:

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

NaN:

js
parseInt("Hello", 8); //   
parseInt("546", 2); //      

-15:

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

4:

js
parseInt(4.7, 10);
parseInt(4.7 * 1e22, 10); //     4
parseInt(0.00000000000434, 10); //     4

224:

js
parseInt("0e0", 16);

ECMAScript 3 ECMAScript 5, , 0, . , . , .

js
parseInt("0e0"); // 0
parseInt("08"); // 0, '8'      .

ECMAScript 5

ECMAScript 5 parseInt , 0. ECMAScript 5 :

parseInt . . 0, 10, , 0x 0X: 16. 16, 0x 0X.

ECMAScript 5 ECMAScript 3, , .

2013 , , .

. :

js
var filterInt = function (value) {
  if (/^(\-|\+)?([0-9]+|Infinity)$/.test(value)) return Number(value);
  return NaN;
};

console.log(filterInt("421")); // 421
console.log(filterInt("-421")); // -421
console.log(filterInt("+421")); // 421
console.log(filterInt("Infinity")); // Infinity
console.log(filterInt("421e+0")); // NaN
console.log(filterInt("421hop")); // NaN
console.log(filterInt("hop1.61803398875")); // NaN
console.log(filterInt("1.61803398875")); // NaN

Specification
ECMAScript 2027 LanguageSpecification
# sec-parseint-string-radix


Web Proxy Viewer  |  New URL  |  Original Page