How to convert NaN to 0 using JavaScript ?

 

NaN (Not a Number) is typically used to indicate an error condition for a function that should return a valid number but can be converted to 0 using JavaScript. In this article we will convert NaN to 0.

Using isNaN() Method: The isNan() method is used to check whether the given number is NaN or not. If isNaN() returns true for "number", it assigns a value of 0.

 


number = NaN;
if (isNaN(number)) number = 0;
console.log(number);


Tags:

Share:

Related posts