Using isNaN() method: The isNan() method is used to check whether the given number is NaN or not. If isNaN() returns true for “number” then it assigns the value 0.
Example: In this example, we will check given number is NaN or not.
Using || Operator: If “number” is any falsy value, it will be assigned to 0.
Example: In this example, we will compare NaN with 0 using || operator
Using ternary operator: Here the number is checked via ternary operator, similar to 1, if NaN it converts to 0.
Example: In this example, we will use the ternary operator to convert NaN to 0.
Note: While executing the code on our IDE or on your browser, check the console (F12) to see the result.
(NaN === 0) ? NaN : 0
Example: In this example, we will convert NaN to 0 by using === operator, first we compare the NaN value with zero if it is true we return NaN in another case we return 0.
Please Login to comment...