Page 194 - JavaScript
P. 194

Number.isNaN(NaN);         // true
         Number.isNaN(0 / 0);       // true
         Number.isNaN('str' - 12);  // true

         Number.isNaN(24);          // false
         Number.isNaN('24');        // false
         Number.isNaN(1 / 0);       // false
         Number.isNaN(Infinity);    // false

         Number.isNaN('str');       // false
         Number.isNaN(undefined);   // false
         Number.isNaN({});          // false


        6

        You can check if a value is NaN by comparing it with itself:


         value !== value;    // true for NaN, false for any other value


        You can use the following polyfill for Number.isNaN():


         Number.isNaN = Number.isNaN || function(value) {
             return value !== value;
         }


        By contrast, the global function isNaN() returns true not only for NaN, but also for any value or
        expression that cannot be coerced into a number:


         isNaN(NaN);         // true
         isNaN(0 / 0);       // true
         isNaN('str' - 12);  // true

         isNaN(24);          // false
         isNaN('24');        // false
         isNaN(Infinity);    // false

         isNaN('str');       // true
         isNaN(undefined);   // true
         isNaN({});          // true


        ECMAScript defines a “sameness” algorithm called SameValue which, since ECMAScript 6, can be
        invoked with Object.is. Unlike the == and === comparison, using Object.is() will treat NaN as
        identical with itself (and -0 as not identical with +0):


         Object.is(NaN, NaN)      // true
         Object.is(+0, 0)         // false

         NaN === NaN              // false
         +0 === 0                 // true


        6

        You can use the following polyfill for Object.is() (from MDN):





        https://riptutorial.com/                                                                             151
   189   190   191   192   193   194   195   196   197   198   199