Page 164 - JavaScript
P. 164

checks if the resulting value is NaN. For this reason, this testing method may cause confusion.

        (*) The "conversion" method is not that simple, see ECMA-262 18.2.3 for a detailed explanation of the algorithm.


        These examples will help you better understand the isNaN() behavior:


         isNaN(NaN);          // true
         isNaN(1);            // false: 1 is a number
         isNaN(-2e-4);        // false: -2e-4 is a number (-0.0002) in scientific notation
         isNaN(Infinity);     // false: Infinity is a number
         isNaN(true);         // false: converted to 1, which is a number
         isNaN(false);        // false: converted to 0, which is a number
         isNaN(null);         // false: converted to 0, which is a number
         isNaN("");           // false: converted to 0, which is a number
         isNaN(" ");          // false: converted to 0, which is a number
         isNaN("45.3");       // false: string representing a number, converted to 45.3
         isNaN("1.2e3");      // false: string representing a number, converted to 1.2e3
         isNaN("Infinity");   // false: string representing a number, converted to Infinity
         isNaN(new Date);     // false: Date object, converted to milliseconds since epoch
         isNaN("10$");        // true : conversion fails, the dollar sign is not a digit
         isNaN("hello");      // true : conversion fails, no digits at all
         isNaN(undefined);    // true : converted to NaN
         isNaN();             // true : converted to NaN (implicitly undefined)
         isNaN(function(){}); // true : conversion fails
         isNaN({});           // true : conversion fails
         isNaN([1, 2]);       // true : converted to "1, 2", which can't be converted to a number


        This last one is a bit tricky: checking if an Array is NaN. To do this, the Number() constructor first
        converts the array to a string, then to a number; this is the reason why isNaN([]) and isNaN([34])
        both return false, but isNaN([1, 2]) and isNaN([true]) both return true: because they get converted
        to "", "34", "1,2" and "true" respectively. In general, an array is considered NaN by isNaN()
        unless it only holds one element whose string representation can be converted to a valid
        number.

        6


        Number.isNaN()

        In ECMAScript 6, the Number.isNaN() function has been implemented primarily to avoid the problem
        of window.isNaN() of forcefully converting the parameter to a number. Number.isNaN(), indeed,
        doesn't try to convert the value to a number before testing. This also means that only values of
        the type number, that are also NaN, return true (which basically means only Number.isNaN(NaN)).


        From ECMA-262 20.1.2.4:

              When the Number.isNaN is called with one argument number, the following steps are
              taken:


                  1.  If Type(number) is not Number, return false.
                  2.  If number is NaN, return true.
                  3.  Otherwise, return false.

        Some examples:




        https://riptutorial.com/                                                                             121
   159   160   161   162   163   164   165   166   167   168   169