Article From:https://segmentfault.com/q/1010000011697318
Question:

Today we have a problem about isNaN () function.

Looking at the definition, it turns out that if it’s a string, it returns true. But when the string is a number, it does return false. Want to know what’s missing here?

Answer 0:

isNaN,”The puzzling and weird behavior mentioned in that section will be judged by Number transformation first.

In general, MDN and ECMA can be trusted.

Answer 1:

isNaNThe values in brackets are implicitly typed, string numbers, string types are converted to number types, and then judged.
But if isNaN (“2005avc”) can not be converted to number type, it will return to true.

Answer 2:

Look at the links below:

mdnChinese description

Answer 3:

First of all, we need to understand why isNaN function exists. This function exists because when you judge whether a value is of type number, you can’t rule out whether the value is NaN, because NaN doesn’t equal any value in js, including himself.

typeof NaN === 'number' //true

Like the example above, it returns true, but actually NaN is not a number, so there’s the isNaN function, which you can write when you judge whether a value is number or not?

function isNumber(num){
    return typeof num === 'number' && !isNaN(num) ? true : false
}

Answer 4:

ESStandard inisNaN Definition

as well asToNumberDefinition

So we can also find out.

isNaN(undefined); // true
isNaN(null); // false

aboutStringIt is literally converted to numbers, so why?

function toNumber(x) { return +x;}
toNumber("0xF"); // 15
toNumber("0o17"); // 15
toNumber("0b1111"); // 15

Leave a Reply

Your email address will not be published. Required fields are marked *