当前位置:网站首页>关于NaN的一些总结
关于NaN的一些总结
2022-06-26 12:32:00 【铁锤妹妹@】
首先明确一点,NaN不是独立的数据类型,而是一个特殊数值,它的数据类型依然属于Number。是not a number的缩写,表示非数字。
typeof NaN // 'number'
1. 出现NaN的原因
可能原因:
- 操作的两个数,类型不一致
var a = 10;
var b ; //undefined
console.log(a + b) //NaN 相当于10 + undefined
- 其中一个值为NaN,计算后仍为NaN
NaN + 1 //NaN
10*NaN //NaN
- 一个不能被解析的数字
Number('abc') //NaN
Number(undefined) //NaN
为防止出错在计算之前console一下,查看计算的数字的具体类型。防止出错。
2. 关于NaN
NaN是唯一一个和自身不相等的值:
NaN === NaN // false
如果想确定这个值是不是NaN,可以利用NaN的特殊性;
值是否与本身不相等(NaN是唯一有这样特性的值)
function isValueNaN(value){
return value !== value
}
3. 如何辨别NaN(非数字)
可以用isNaN()来判断一个数值是不是非数字,并不是用来判断是不是NaN这个值,这个地方可能刚看有点绕,多看几遍就能体会了;
isNaN(undefined) //true
isNaN(NaN) //true
isNaN('0') //false
为什么说isNaN() 不是判断NaN这个值呢?
因为isNaN() 对非数字不起作用,他首先是Number()把值转成数字,转换结果可能是NaN,然后就返回true
4. 关于isNaN()
项目中使用 isNaN(undfined)结果是true,不是很明白,百度问题发现
使用 isNaN() 判断是不是 NaN 时,此方法会使用 Number() 方法做一个转换,任何非数字都会被 强制类型 转换,undefined 被转换后为 NaN ,所以显示 true;
isNaN(xxx) 其实与 isNaN(Number(xxx)) 等价
值得一提的是,isNaN() 转换时除了null,数组类型,空字符串和布尔值会被强制转换为数值0或1,返回false之外;其余非数字类型都会被转为NaN,返回true
举例:
isNaN(NaN) //true
isNaN(undefined) //true
isNaN({
}) //true
isNaN(true) //false true 会转换为 1,false 转换为 0
isNaN(null) //false null转为0
isNaN(66) //false
//strings
isNaN('66') //false 被转换成数值66
isNaN('66.88') //false 被转换成数值66.88
isNaN("") //false 空字符串转为0,是数字
isNaN(" ") //false 包含空格的字符串被转换成0
//dates
isNaN(new Date()) //false
isNaN(new Date().toString()) //true 转成了字符串文字,非数字
isNaN("kkk") //true "kkk"不能转换成数字,值是NaN
5. isNaN()和NaN的区别
isNaN() 是一个全局方法(挂在window对象上的方法),它的作用是检查一个值是否能被Number() 成功转换。能转换成功,就返回false,否则返回true。
是Boolean类型
NaN是一个特殊数值,不是数据类型。
当一个字符串不能被Number,parseInt或parseFloat成功转换时,就返回NaN
NaN是唯一一个和自身不相等的值, NaN !== NaN
6. 关于隐式转换
减号“-” 可理解为减法运算 (-0转换成数字)
加号“+” 可理解为字符串拼接(+“”转换成字符串)

边栏推荐
- 7-16 货币系统Ⅰ
- SQL injection in Pikachu shooting range
- leetcode 715. Range module (hard)
- 2022 edition of investment analysis and "fourteenth five year plan" development prospect forecast report of China's switchgear industry
- PHP get directory size
- 2022 China smart bathroom cabinet Market Research and investment Competitiveness Analysis Report
- PHP calculates excel coordinate values, starting with subscript 0
- 2021 q3-q4 investigation report on the use status of kotlin multiplatform
- Seven major trends deeply affecting the U.S. consumer goods industry in 2022
- The loss of female scientists
猜你喜欢

4. N queen problem

Laravel+gatewayworker completes the im instant messaging and file transfer functions (Chapter 4: server debugging errors)

Implementing mixins scheme in applet
![[redis series] redis learning 16. Redis Dictionary (map) and its core coding structure](/img/d2/a6cbb0abe9e04c412d1f6021430528.png)
[redis series] redis learning 16. Redis Dictionary (map) and its core coding structure

国际美妆业巨头押注中国
![[solved] laravel completes the scheduled job task (delayed distribution task) [execute a user-defined task at a specified time]](/img/13/c2c63333a9e5ac08b339449ea17654.jpg)
[solved] laravel completes the scheduled job task (delayed distribution task) [execute a user-defined task at a specified time]

PHP uses laravel pay component to quickly access wechat jsapi payment (wechat official account payment)

MS17_ 010 utilization summary

2、 MySQL Foundation

SQL injection in Pikachu shooting range
随机推荐
Thinkphp5 query report: sqlstate[hy093]: invalid parameter number
China Medical Grade hydrogel market supply and demand research and prospect analysis report 2022 Edition
Wechat applet wx Request request encapsulation
24 database interview questions that must be mastered!
CG bone animation
Refined operation, extending the full life cycle value LTV
VMware虚拟机 桥接模式 无法上网 校园网「建议收藏」
Redis cannot connect to the server through port 6379
HUST網絡攻防實踐|6_物聯網設備固件安全實驗|實驗二 基於 MPU 的物聯網設備攻擊緩解技術
Comparison of latest mobile phone processors in 2020 (with mobile phone CPU ladder diagram)
New routing file in laravel framework
Operation analysis and investment prospect research report of China's organic chemical raw material manufacturing industry 2022-2028
女性科学家的流失
One click deployment CEPH script
11、 Box styles and user interface
Omni channel member link - tmall member link 3: preparation of member operation content
What should I do from member labels to portraits?
[solved] data duplication or data loss after laravel paginate() paging
Jmeter响应时间和tps监听器使用教程
Lintcode 130 · 堆化