Confusion between isNaN and Number.isNaN in javascript

I have a confusion about how NaN works. I executed isNaN(undefined) , it returned true . But if I use Number.isNaN(undefined) , it returns false . So which one should I use. Also, why is there such a mismatch as a result.

+10
source share
4 answers

To quote a ponyfoo article on numbers in ES6 :

Number.isNaN is almost identical to the global method isNaN ES5. Number.isNaN returns whether the provided value is equal to NaN. This is a very different question from "is this not a number?"

So, isNaN just checks to see if the passed value is a number or cannot be converted to a number. Number.isNaN , on the other hand, checks if the value of NaN is equal (it uses a different algorithm than === though).

The string 'ponyfoo' , for example, is not a number and cannot be converted to a number, but it is not NaN .

Example:

 Number.isNaN({}); // <- false, {} is not NaN Number.isNaN('ponyfoo') // <- false, 'ponyfoo' is not NaN Number.isNaN(NaN) // <- true, NaN is NaN Number.isNaN('pony'/'foo') // <- true, 'pony'/'foo' is NaN, NaN is NaN isNaN({}); // <- true, {} is not a number isNaN('ponyfoo') // <- true, 'ponyfoo' is not a number isNaN(NaN) // <- true, NaN is not a number isNaN('pony'/'foo') // <- true, 'pony'/'foo' is NaN, NaN is not a number 
+17
source

I found that if you want to check if something is numeric (or not), then the combination of Number.isNaN() with Number.parseInt() or Number.parseFloat() (depending on what you expect) covers most use cases:

consider the following questions: check a bunch of different input vars against several - these are numerical tests:

 r = [NaN, undefined, null, false, true, {}, [], '', ' ', 0, 1, '0', '1'] .map(function(v){return [ v, isNaN(v), Number.isNaN(v), Number.isInteger(v), Number.parseInt(v, 10), Number.isNaN( Number.parseInt(v, 10)) ];}); console.table(r); // or if console.table() not available: r.join('\n', function(v){v.join(',')} ); 

result:

 NaN , true , true , false, NaN, true undefined, true , false, false, NaN, true null , false, false, false, NaN, true false , false, false, false, NaN, true true , false, false, false, NaN, true Object , true , false, false, NaN, true Array(0) , false, false, false, NaN, true '' , false, false, false, NaN, true ' ' , false, false, false, NaN, true 0 , false, false, true , 0 , false 1 , false, false, true , 1 , false '0' , false, false, false, 0 , false '1' , false, false, false, 1 , false 

Pay attention to the last column, which, as a rule, I want in my experience.

+4
source
  • isNaN converts the argument to Number and returns true if the resulting value is NaN .
  • Number.isNaN does not convert the argument; it returns true when the argument is Number and NaN .

So which one should I use.

I assume that you are trying to check if a value is something like a number. In this case, the answer is neither . These functions check if the value of IEEE-754 is not a number. Period. For example, this is clearly wrong:

 var your_age = ""; // user forgot to put in their age if (isNaN(your_age)) { alert("Age is invalid. Please enter a valid number."); } else { alert("Your age is " + your_age + "."); } // alerts "Your age is ." // same result when you use Number.isNaN above 

In addition, why the result is such a discrepancy.

As explained above, Number.isNaN will immediately return false if the argument is not Number , and isNaN converts the value to Number . This changes the result. Some examples:

  | Number.isNaN() | isNaN() ----------------+----------------------------+----------------------- value | value is a Number | result | Number(value) | result ----------------+-------------------+--------+---------------+------- undefined | false | false | NaN | true {} | false | false | NaN | true "blabla" | false | false | NaN | true new Date("!") | false | false | NaN | true new Number(0/0) | false | false | NaN | true 
+2
source

Soon

isNaN()

will check if the conversion of the value to a number (type) has worked

For example, 'abc'


Number.isNaN()

will check if the given value type is Number, but not a real number

For example: 'bb' / 33

+1
source

Source: https://habr.com/ru/post/1233841/


All Articles