I am learning ES6 recently and have some questions about numerical expansion.
ES6 provides two methods on the Number object: Number.isFinite() and Number.isNaN()
The difference from ES5 is that it cancels calling Number() once to convert the non-numeric value into a numerical value before judging.
Note: The difference between the two methods and the traditional isFinite() and isNaN() methods is that the traditional method first calls Number() to convert the non-numeric value into a numerical value, and then judges. These two methods are only valid for numerical values, and will return false for non-numeric values.
I don’t quite understand why this is done. I think it wouldn’t be better to call Number() once to convert the non-numeric value into a numerical value and then make a judgment?
phpcn_u15822017-05-18 10:54:30
You can check out MDN’s description
As for isFinite, you can compare Number.isFinite(true) and isFinite(true) to know. Of course, Number.isFinite("") and isFinite("") will also work.
黄舟2017-05-18 10:54:30
These two functions are used to determine whether a value is limited or NaN on Number. The premise is a number. If internal hidden operations such as string are converted into numbers, the meaning they want to directly express will be lost, and it will also bring For some other problems, it becomes possible to judge not only numbers, but also types such as strings.
Reference link:
es6
isNaN