当前位置:网站首页>ES6:数值的扩展
ES6:数值的扩展
2022-08-11 08:57:00 【潮汐未见潮落】
Number.isFinite() 和 Number.isNaN()
ES6 在 Number 对象上,新提供了 Number .isFinite() 和 Number.isNaN() 两个方法。
Number.isFinite()
Number .isFinite()用来检查一个数值是否为有限的(finite),即不是 Infinity。
注意,如果参数类型不是数值,Number .isFinite() 一律返回 false
Number.isFinite(15); // true
Number.isFinite(0.8); // true
Number.isFinite(NaN); // false
Number.isFinite(Infinity); // false
Number.isFinite(-Infinity); // false
Number.isFinite('foo'); // false
Number.isFinite('15'); // false
Number.isFinite(true); // falseNumber.isNaN()
Number.isNaN() 用来检查一个值是否为 NaN
注意:如果参数类型不是 NaN,Number.isNaN 一律返回 false。
Number.isNaN(NaN) // true
Number.isNaN(15) // false
Number.isNaN('15') // false
Number.isNaN(true) // false
Number.isNaN(9/NaN) // true
Number.isNaN('true' / 0) // true
Number.isNaN('true' / 'true') // true与传统方法的区别
它们与传统的全局方法 isFinite()和 isNaN()的区别在于,传统方法先调用 Number()将非数值的值转为数值,再进行判断,而这两个新方法只对数值有效,Number.isFinite()对于非数值一律返回 false, Number.isNaN()只有对于 NaN才返回true,非NaN一律返回false。
isFinite(25) // true
isFinite("25") // true
Number.isFinite(25) // true
Number.isFinite("25") // false
isNaN(NaN) // true
isNaN("NaN") // true
Number.isNaN(NaN) // true
Number.isNaN("NaN") // false
Number.isNaN(1) // falseNumber.parseInt(), Number.parseFloat()
ES6 将全局方法 parseInt()和 parseFloat(),移植到 Number对象上面,行为完全保持不变。
这样做的目的,是逐步减少全局性方法,使得语言逐步模块化。
// ES5的写法
parseInt('12.34') // 12
parseFloat('123.45#') // 123.45
// ES6的写法
Number.parseInt('12.34') // 12
Number.parseFloat('123.45#') // 123.45Number.parseInt === parseInt // true
Number.parseFloat === parseFloat // true文章如有错误,恳请大家提出问题,本人不胜感激 。 不懂的地方可以评论,我都会一一回复
文章对大家有帮助的话,希望大家能动手点赞鼓励,大家未来一起努力 长路漫漫,道阻且长
边栏推荐
- Detailed Explanation of the Level 5 Test Center of the Chinese Institute of Electronics (1)-string type string
- Typescript基本类型---下篇
- SDUT 2877: angry_birds_again_and_again
- Jupyter Notebook 插件 contrib nbextension 安装使用
- 刷题错题录2-向上取整、三角形条件、字符串拼接匹配、三数排序思路
- Go 语言的诞生
- 盘点四个入门级SSL证书
- DataGrip配置OceanBase
- Nuget找不到包的问题处理
- Initial use of IDEA
猜你喜欢

Rust从入门到精通06-函数

Unity3D - modification of the Inspector panel of the custom class
![ASP.NET Core 6框架揭秘实例演示[32]:错误页面的集中呈现方式](/img/c9/93ab353c4908adaaae0da2cc3b6a3c.png)
ASP.NET Core 6框架揭秘实例演示[32]:错误页面的集中呈现方式

Initial use of IDEA

研发了 5 年的时序数据库,到底要解决什么问题?

magical_spider远程采集方案

The no-code platform helps Zhongshan Hospital build an "intelligent management system" to realize smart medical care

VoLTE基础自学系列 | 3GPP规范解读之Rx接口(上集)

Analysis of the Status Quo of Enterprise Server Host Reinforcement

IPQ4019/IPQ4029 support WiFi6 MiniPCIe Module 2T2R 2×2.4GHz 2x5GHz MT7915 MT7975
随机推荐
nodejs微服务中跨域,请求,接口,参数拦截等功能
关于ts中的指针问题call,bind, apply
仙人掌之歌——大规模高速扩张(1)
Getting Started with Kotlin Algorithm to Calculate the Number of Daffodils
1.3版本自定义TrainOneStepCell报错
用 Antlr 重构脚本解释器
js将table生成excel文件并去除表格中的多余tr(js去除表格中空的tr标签)
For the first time, I suspect that there is a bug in selenium4 because the iframe element is not found?
Contrastive Learning Series (3)-----SimCLR
Unity3D - modification of the Inspector panel of the custom class
兼容并蓄广纳百川,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang复合容器类型的声明和使用EP04
Notable NFT development trends in 2022
磁盘管理:磁盘结构
Typescript基本类型---上篇
SDUT 2877: angry_birds_again_and_again
欧拉函数(用欧拉筛法求欧拉函数)
Nuget找不到包的问题处理
Nuget can't find the package problem
基于 VIVADO 的 AM 调制解调(1)方案设计
工业检测深度学习方法综述