NaN in JavaScript

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN

The global NaN property is a value representing Not-A-Number.

 

NaN is a property of the global object.

The initial value of NaN is Not-A-Number — the same as the value of Number.NaN.

In modern browsers, NaN is a non-configurable, non-writable property.

Even when this is not the case, avoid overriding it.

 

It is rather rare to use NaN in a program.

It is the returned value when Math functions fail (Math.sqrt(-1)) or when a function trying to parse a number fails (parseInt("blabla")).

 

Testing against NaN

NaN compares unequal (via ==, !=, ===, and !==) to any other value -- including to another NaN value.

Use Number.isNaN() or isNaN() to most clearly determine whether a value is NaN.

Or perform a self-comparison: NaN, and only NaN, will compare unequal to itself.

 

NaN === NaN;        // false Number.NaN === NaN; // false isNaN(NaN); // true isNaN(Number.NaN); // true function valueIsNaN(v) { return v !== v; } valueIsNaN(1); // false valueIsNaN(NaN); // true valueIsNaN(Number.NaN); // true

 

However, do note the difference between isNaN() and Number.isNaN(): the former will return true if the value is currently NaN, or if it is going to be NaN after it is coerced to a number, while the latter will return true only if the value is currently NaN:

isNaN('hello world'); // returns 'true'. Number.isNaN('hello world'); // returns 'false'.

 

判断一个参数是否是数字

https://www.codewars.com/kata/is-it-a-number/train/javascript

https://stackoverflow.com/questions/18082/validate-decimal-numbers-in-javascript-isnumeric

https://stackoverflow.com/questions/6449611/how-to-check-whether-a-value-is-a-number-in-javascript-or-jquery

https://stackoverflow.com/questions/1303646/check-whether-variable-is-number-or-string-in-javascript

function isDigit(s) {
return !isNaN(parseFloat(s)) && isFinite(s);
}

 

或者是

function isDigit(s) {
    return s==(parseFloat(s)) ;
}

 

或者是

function isNumber(n) { return !isNaN(parseFloat(n)) && !isNaN(n - 0) }

 

转载于:https://www.cnblogs.com/chucklu/p/9212677.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值