Skip to main content

JavaScript convert NaN to 0 using double tilde

In computing, NaN (not a number) is a numeric data type that means a value is undefined or cannot be represented.

To convert NaN to 0 in JavaScript, use the double tilde (~~) operator. The double tilde is a bitwise operator that does a bitwise NOT operation, essentially turning non-numeric data to zero.

Example:

var sum = NaN;
let ans = ~~sum;
 
console.log(ans); // Output: 0