How is NaN converted to Boolean in JavaScript?



In this article, we will learn how to convert NaN to Boolean. NaN in JavaScript means Not a Number, whose type is a Number but actually, it is not a number. To Convert, the NaN to a Boolean we use Multiple methods some of which are discussed below.

  • Using the Boolean() Function
  • Using the !! Operator
  • Using the NOT Operator and isNaN() Method

Using the Boolean() Function

The Boolean function is used to get the Boolean value (True or False) of any variable, condition, or object. To convert the NaN into Boolean we just need to pass the NaN in the Boolean function.

Syntax

Following is the syntax to convert NaN to Boolean:

Boolean(NaN)

Here Boolean() function returns false.

Example 1

In this example, we created a variable named bool and passed the NaN inside the Boolean function and stored the returned value to bool, and printed the result.

<html> <body> <p>Convert NaN to Boolean </p> <p id ="result"></p> <script> let bool = Boolean(NaN) document.getElementById("result").innerHTML += bool +"<br>"; document.getElementById("result").innerHTML += typeof bool </script> </body> </html>

Using the !! Operator

The Logical NOT operator followed by another Logical NOT operator is a simple and elegant method to convert the NaN to Boolean. The first logical operator converts the value to the Boolean and another logical operator reverses the value returned by the first operator.

Syntax

!!object

Let's break down the operator into two-part with an example:

Example 2

In this example, we created a variable named x and stored Tutorials point to it and created another variable named x1 in which we stored the value of !x, here the logical operator converts the value to the Boolean and another operator(x2) reverses the result given by the first operator(x1).

<html> <body> <p id ="output"></p> <script> var x = "Tutorials Point" var x1 = !x; // false var x2 = !!x; // true document.getElementById("output").innerHTML = x1 + " <br>"; document.getElementById("output").innerHTML += x2 </script> </body> </html>

Example 3

In this example, we will convert the NaN to Boolean.

<html> <body> <p>Convert NaN to Boolean</p> <p id ="output"></p> <script> let bool = !!NaN; document.getElementById("output").innerHTML += bool +"<br>"; document.getElementById("output").innerHTML += typeof bool </script> </body> </html>

Using the NOT Operator and isNaN() Method

We can use the NOT operator and isNaN() method to convert NaN to Boolean. The isNaN() method returns true if the value is NaN else false. The isNaN() method converts the value to the number before it tests the value. The NOT(!) operator converts true to false.

Syntax

!isNaN(NaN)

Here isNaN() returns true as the value is NaN and finally the ! operator converts the true to false.

Example 4

In the example below, we convert NaN to boolean using the isNaN() method. We also test the type of the variable after converting it to boolean.

<html> <body> <p>Convert NaN to Boolean</p> <p id ="output"></p> <script> let bool = !isNaN(NaN); document.getElementById("output").innerHTML += bool +"<br>"; document.getElementById("output").innerHTML += typeof bool </script> </body> </html>

As we have mentioned three methods here are high performance. The Boolean function is widely used by some developers while the !! operator method seems a little non-readable and some developers have no knowledge about it. If we talk about the speed of both of the solutions the !! operator is a little faster than the Boolean function. The third method we discussed is to use the ! operator and isNaN() in combination. It converts the NaN to boolean as false.


Advertisements