Page 163 - JavaScript
P. 163

Chapter 17: Built-in Constants




        Examples



        Operations that return NaN


        Mathematical operations on values other than numbers return NaN.


         "a" + 1
         "b" * 3
         "cde" - "e"
         [1, 2, 3] * 2


        An exception: Single-number arrays.


         [2] * [3]  // Returns 6


        Also, remember that the + operator concatenates strings.


         "a" + "b"  // Returns "ab"


        Dividing zero by zero returns NaN.


         0 / 0         // NaN


        Note: In mathematics generally (unlike in JavaScript programming), dividing by zero is not
        possible.


        Math library functions that return NaN


        Generally, Math functions that are given non-numeric arguments will return NaN.


         Math.floor("a")


        The square root of a negative number returns NaN, because Math.sqrt does not support imaginary
        or complex numbers.


         Math.sqrt(-1)



        Testing for NaN using isNaN()


        window.isNaN()

        The global function isNaN() can be used to check if a certain value or expression evaluates to NaN.
        This function (in short) first checks if the value is a number, if not tries to convert it (*), and then




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