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.
1 2 3 4 5 6 7 8 9 | 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:
1 2 | 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/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) }
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2017-06-22 Whats the difference between git reset --mixed, --soft, and --hard?
2017-06-22 Import .bak file to a database in SQL server
2017-06-22 Resize图片