Метод Number.isInteger()
визначає, чи є передане значення цілим числом.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Синтаксис
Number.isInteger(value)
Параметри
value
- Значення для перевірки.
Повертає
Значення типу Boolean
, що вказує, чи є надане значення цілим числом.
Опис
Якщо параметр є цілим числом, повертає true
, інакше вертає false
. Якщо значення дорівнює NaN
або Infinity
, повертає false
. Цей метод також повертає true
для чисел з плаваючою крапкою, які можуть бути представлені як цілі.
Поліфіл
Number.isInteger = Number.isInteger || function(value) {
return typeof value === 'number' &&
isFinite(value) &&
Math.floor(value) === value;
};
Приклади
Використання isInteger
Number.isInteger(0); // true
Number.isInteger(1); // true
Number.isInteger(-100000); // true
Number.isInteger(99999999999999999999999); // true
Number.isInteger(0.1); // false
Number.isInteger(Math.PI); // false
Number.isInteger(NaN); // false
Number.isInteger(Infinity); // false
Number.isInteger(-Infinity); // false
Number.isInteger('10'); // false
Number.isInteger(true); // false
Number.isInteger(false); // false
Number.isInteger([1]); // false
Number.isInteger(5.0); // true
Number.isInteger(5.000000000000001); // false
Number.isInteger(5.0000000000000001); // true
Специфікації
Підтримка веб-переглядачами
BCD tables only load in the browser
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Див. також
- Об'єкт
Number
, до якого належить цей метод.