当前位置:网站首页>关于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转换成数字)
加号“+” 可理解为字符串拼接(+“”转换成字符串)

边栏推荐
- Wechat applet wx Request request encapsulation
- How can we reach members more effectively?
- Msvcr110 not found DLL, unable to continue code execution Solution for startup
- On the use of protostaff [easy to understand]
- 十大券商有哪些?手机开户安全么?
- What software is flush? Is online account opening safe?
- 2022 edition of China's energy and chemical industry market in-depth investigation and investment feasibility analysis report
- 开通证券账户需要注意事项 开户安全吗
- Fengshentai old shooting range Kali series
- Lintcode 130 · 堆化
猜你喜欢

Realize microservice load balancing (ribbon)

【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构

JMeter response time and TPS listener tutorial

leetcode 715. Range module (hard)

Laravel subdomain accesses different routing files and different modules
![[solved] data duplication or data loss after laravel paginate() paging](/img/68/7bf51bbf893a91bee24f5f7d4a369f.jpg)
[solved] data duplication or data loss after laravel paginate() paging

Fengshentai old shooting range Kali series

Build Pikachu shooting range and introduction

HUST network attack and defense practice | 6_ IOT device firmware security experiment | Experiment 2 MPU based IOT device attack mitigation technology

Pratique de l'attaque et de la défense du réseau HUST | 6 Expérience de sécurité du microprogramme de l'équipement IOT | expérience 2 technologie d'atténuation des attaques de l'équipement IOT basée s
随机推荐
Analysis report on dynamic research and investment planning suggestions of China's laser medical market in 2022
【概率论】条件概率、贝叶斯公式、相关系数、中心极限定理、参数估计、假设检验
Vulnerability scanning and reverse osmosis of Internet anti artifact
One click deployment CEPH script
Laravel subdomain accesses different routing files and different modules
[redis series] redis learning 16. Redis Dictionary (map) and its core coding structure
Scala-day03- operators and loop control
Laravel+gatewayworker completes the im instant messaging and file transfer functions (Chapter 4: server debugging errors)
初探Protostuff的使用[通俗易懂]
How do consumer goods enterprises formulate membership interests?
Thinkphp5 query report: sqlstate[hy093]: invalid parameter number
Analysis report on the "fourteenth five year plan" and investment prospect of China's pharmaceutical equipment industry 2022-2028
fastjson的JSONArray和JSONObject[通俗易懂]
Leetcode 78. 子集 and 90. 子集 II
Microservice governance (nocas)
PHP uses laravel pay component to quickly access wechat jsapi payment (wechat official account payment)
Five strategies and suggestions of member marketing in consumer goods industry
File decryption in webgame development
CG bone animation
Nodejs get get/post request parameters