JavaScript Number Type Checker
Number.isInteger
// static 方法
Number.isInteger(value)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger
demo
function fits(x, y) {
return Number.isInteger(y / x);
}
console.log(fits(5, 10));
// true
console.log(fits(5, 11));
// false
Number.isSafeInteger
The safe integers consist of all integers from -(253 - 1) inclusive to 253 - 1 inclusive (± 9007199254740991 or ± 9,007,199,254,740,991).
53
=== 2^53
// static 方法
Number.isSafeInteger(testValue)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger
function warn(x) {
if (Number.isSafeInteger(x)) {
return 'Precision safe.';
}
return 'Precision may be lost!';
}
console.log(warn(Math.pow(2, 53)));
// "Precision may be lost!"
console.log(warn(Math.pow(2, 53) - 1));
// "Precision safe."
Number.isNaN
value is NaN & type is Number
// static 方法
Number.isNaN(value)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN
demo
function typeOfNaN(x) {
if (Number.isNaN(x)) {
return 'Number NaN';
}
if (isNaN(x)) {
// string NaN?
return 'NaN';
}
}
console.log(typeOfNaN('100F'));
// "NaN"
console.log(typeOfNaN(NaN));
// "Number NaN"
Number.isFinite
// static 方法
Number.isFinite(value)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isFinite
NaN
null
undefined
refs
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!