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

边栏推荐
- The transformation of enterprise customers' digital assets needs to suit the case
- Mysql8 master-slave replication
- [solved] laravel completes the scheduled job task (delayed distribution task) [execute a user-defined task at a specified time]
- [redis series] redis learning 16. Redis Dictionary (map) and its core coding structure
- Scala-day06- pattern matching - Generic
- 7-1 数的范围
- Consumer goods enterprises, four pain points of member marketing
- One click deployment of your own community forum
- International beauty industry giants bet on China
- 7-3 最低通行费
猜你喜欢

International beauty industry giants bet on China
![[graduation season · advanced technology Er] I remember the year after graduation](/img/e7/8e1dafa561217b77a3e3992977a8ec.png)
[graduation season · advanced technology Er] I remember the year after graduation

The laravel dingo API returns a custom error message
![[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]

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

Several rare but useful JS techniques

PHP laravel+gatewayworker completes im instant messaging and file transfer (Chapter 1: basic configuration)
The loss of female scientists

MySQL optimization - index (what is an index?)

PHP uses laravel pay component to quickly access wechat jsapi payment (wechat official account payment)
随机推荐
Cross platform members get through the two channels of brand Ren Du
redis通过6379端口无法连接服务器
A most practical arbitrage wizard EA [2022 modified version]
开通证券账户需要注意事项 开户安全吗
Xiaolong 888 was released, Xiaomi 11 was launched, and 14 manufacturers carried it in the first batch!
Current situation investigation and investment prospect forecast analysis report of China's electrolytic copper market from 2022 to 2028
2016年四川省TI杯电子设计竞赛B题
【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构
PHP uses laravel pay component to quickly access wechat jsapi payment (wechat official account payment)
Oracle锁表查询和解锁方法
Encapsulate request request of uni app
【概率论】条件概率、贝叶斯公式、相关系数、中心极限定理、参数估计、假设检验
Installing MySQL under Linux (RPM package installation)
Polarismesh series articles - concept series (I)
PHP unit conversion
Common problems and Thoughts on member operation management
Basic principle of MOS tube and important knowledge points of single chip microcomputer
Mysql8 master-slave replication
Why is password salt called "salt"? [Close] - why is a password salt called a "salt"? [closed]
Deep thinking from senior member managers