How to use ternary operator In Javascript
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.
javascript
number = NaN; number = number ? number : 0; console.log(number); |
Output
0
Note: While executing the code on our IDE or on your browser, check the console (F12) to see the result.
How to convert NaN to 0 using JavaScript ?
NaN (Not a Number) is usually used to indicate an error condition for a function that should return a valid number but it can be converted to 0 using JavaScript. We will discuss how to convert the NaN to 0.
Below are the approaches used to convert NaN to 0 using JavaScript:
Table of Content
- Using isNaN() method
- Using || Operator
- Using ternary operator
- Using Strictly Equality(===) operator
- Using the Double Tilde (~~) Operator