How to use Strictly Equality(===) operator In Javascript
JavaScript Strict Equality Operator is used to compare two operands and return true if both the value and type of operands are the same. to convert NaN into zero using the === operator first comparing NaN value with zero if the comparison is successful NaN is returned, otherwise, zero is returned.
Syntax:
(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.
Javascript
let a = NaN; a = (a === 0) ? a : 0; console.log( "Result: " + a); |
Result: 0
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