Basic concept: the isNaN() function is used to determine whether a value isNaN.

Basic examples:

/ / 1
isNaN(NaN);       // true

/ / 2
isNaN(true);      // false
isNaN(false);	  // true

/ / 3
isNaN(37);        // false
isNaN('37');      // false

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

/ / 5
isNaN({});        // true
isNaN({name: 'LinYY'});        // true
Copy the code

Consider: why is null false in 4 above? Undefined returns true? The object is false? To answer this question, let’s look at a few related examples

The original type

Basic types of

  • character
isNaN('12')    // Number('12') > 12 -> false
isNaN('12,)   // ==>Number('12, 23') -> 12,23 -> true
Copy the code
  • digital
isNaN(12)    // Number(12) > 12 -> false
Copy the code

Reference types

  • object
let o = {}
isNaN(o)   // ==> true

o.toString()   // ==> "[object Object]", Number("[object Object]") ==> true
Copy the code
  • An array of
let arr = [12.23]
isNaN(arr)    // ==> true

arr.toString()   // ==> "12, 23", 所以 Number("12, 23") ==> true
Copy the code
  • data
isNaN(new Date());                // false
isNaN(new Date().toString());     // true
Copy the code

Explain the above questions

  • null
isNaN(null) // false because Number(null) ==> 0
Copy the code
  • undefined
isNaN(undefined) / / true. Because Number(undefined) ==> NaN
Copy the code
  • Reference types
isNaN(' ') // false because Number('') -> 0.
/ / in the same way:
isNaN([]) // false because [].toString() -> ", Number(") -> 0
isNaN([12.23]) // true because [12, 23].toString()->"[object object]", Number("[object object]") ==> true
Copy the code

Summary: isNaN() is used in two cases

  • A reference to the data type object is first converted to a string using the toString method and then to a Number type using the Number method
  • Other primitive types are converted directly to numeric types using the Number method

How do you tell if a number is a significant number?

  • Like this?
    if(isNaN(param) == NaN) {console.log('Not a significant number')}Copy the code

This is obviously wrong, because NaN == NaN is also not equal, so this code will never execute

  • Correct use method
        if(isNaN(param)){
        console.log('Not a significant number')}Copy the code