当前位置:网站首页>ES6: Expansion of Numerical Values
ES6: Expansion of Numerical Values
2022-08-11 09:20:00 【no tide】
Number.isFinite() and Number.isNaN()
ES6 provides new methods Number.isFinite() and Number.isNaN() on the Number object.
Number.isFinite()
Number .isFinite() is used to check if a number is finite, i.e. not Infinity.
Note, if the parameter type is not numeric, Number .isFinite() will always return false
Number.isFinite(15); // trueNumber.isFinite(0.8); // trueNumber.isFinite(NaN); // falseNumber.isFinite(Infinity); // falseNumber.isFinite(-Infinity); // falseNumber.isFinite('foo'); // falseNumber.isFinite('15'); // falseNumber.isFinite(true); // false
Number.isNaN()
Number.isNaN() Used to check if a value is NaN
Note: If the parameter type is not NaN
, Number.isNaN
will always beReturns false
.
Number.isNaN(NaN) // trueNumber.isNaN(15) // falseNumber.isNaN('15') // falseNumber.isNaN(true) // falseNumber.isNaN(9/NaN) // trueNumber.isNaN('true' / 0) // trueNumber.isNaN('true' / 'true') // true
Differences from traditional methods
They are the same as the traditional global methods isFinite()
and isNaN()
The difference is that the traditional method calls Number()
converts non-numeric values to numeric values,Then judge, and these two new methods are only valid for numerical values, Number.isFinite()
For non-numeric values, always return false
, Number.isNaN()
is only returned for
, not NaN
trueNaN
Always return false
.
isFinite(25) // trueisFinite("25") // trueNumber.isFinite(25) // trueNumber.isFinite("25") // falseisNaN(NaN) // trueisNaN("NaN") // trueNumber.isNaN(NaN) // trueNumber.isNaN("NaN") // falseNumber.isNaN(1) // false
Number.parseInt(), Number.parseFloat()
ES6 places the global methods parseInt()
and parseFloat()
, ported to Number
object, the behavior remains completely unchanged.
The purpose of this is to gradually reduce global methods and make the language gradually modular.
// ES5 writingparseInt('12.34') // 12parseFloat('123.45#') // 123.45// ES6 writingNumber.parseInt('12.34') // 12Number.parseFloat('123.45#') // 123.45
Number.parseInt === parseInt // trueNumber.parseFloat === parseFloat // true
If there are any mistakes in the article, I urge you to ask questions, I would greatly appreciate it.If you don't understand, you can comment and I will reply.
If the article is helpful to everyone, I hope everyone can give it a thumbs up and encouragement, and everyone will work together in the future. The road is long, and the road is long and long
边栏推荐
猜你喜欢
随机推荐
SDUT 2877: angry_birds_again_and_again
大佬们,我有一个MySQL source 通过旁路分流分了两个流,然后转表,现在想sink到两个hb
Unity3D - modification of the Inspector panel of the custom class
无代码平台助力中山医院搭建“智慧化管理体系”,实现智慧医疗
pycharm中绘图,显示不了figure窗口的问题
框架外的PHP读取.env文件(php5.6、7.3可用版)
nodejs worker_threads的事件监听问题
Typescript基本类型---下篇
安装ES7.x集群
Contrastive Learning Series (3)-----SimCLR
1002 A+B for Polynomials
三次握手与四次挥手
当你领导问你“还有其他的么”
WiFi cfg80211
《价值》读书与投资
tensorflow 基础操作1(tensor 基本属性 , 维度变换,数学运算)
mysql添加用户以及设置权限
Oracle database use problems
基于 VIVADO 的 AM 调制解调(3)仿真验证
@RequiredArgsConstructor注解