Phương thức Number.isInteger()
xác định xem giá trị truyền vô có phải là một integer hay không.
Cú pháp
Number.isInteger(value)
Parameters
value
- Giá trị cần kiểm tra xem nó có phải là một integer hay không.
Mô tả
Nếu giá trị mà bạn truyền vô trong phương thức này là một integer, kết quả trả về sẽ là true
, ngược lại thì trả về false
. Nếu giá trị đó là NaN
hoặc các giá trị Infinity thì đương nhiên vẫn trả về false
.
Các ví dụ
Number.isInteger(0); // true
Number.isInteger(1); // true
Number.isInteger(-100000); // true
Number.isInteger(0.1); // false
Number.isInteger(Math.PI); // 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
Polyfill
Number.isInteger = Number.isInteger || function(value) {
return typeof value === "number" &&
isFinite(value) &&
Math.floor(value) === value;
};
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Number.isInteger' in that specification. |
Standard | Initial definition. |
ECMAScript (ECMA-262) The definition of 'Number.isInteger' in that specification. |
Living Standard |
Trình duyệt hỗ trợ
BCD tables only load in the browser
Xem thêm
- Các đối tượng thuộc
Number
.