How is NaN converted to String in JavaScript?


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

  • Using the String() Method
  • Using the toString Method
  • Using the || Operator
  • Using the isNaN() Method
  • Using Ternary Operator

Using the String() Method

The String() method in JavaScript is used to convert different values to String. To convert the NaN into String Just pass the NaN to this method. Here is the example to convert NaN to string using this approach.

Syntax

String( NaN )

The String() method returns a string with a value converted to String.

Example

In the example below, we convert the NaN to a string using the String() method. We pass NaN as an argument for this method. We also test the type after conversion.

<html> <head> <title> Example: Convert NaN to String</title> </head > <body> <p> Convert NaN to String using String() Method</p> <p id ="output"></p> <script> let str = String(NaN); document.getElementById("output").innerHTML += str +"<br>"; document.getElementById("output").innerHTML += typeof str </script> </body> </html>

Using the toString method

The toString method is used to convert any object into a String. To convert the NaN into String using the NaN just apply the toString method to NaN. Here is the example to convert NaN to string using this approach.

Syntax

NaN.toString()

Example

In the example below, we convert the NaN to a String using the NaN.toString() method. We pass NaN as an argument for this method. We also test the type after conversion.

<html> <head> <title> Example: Convert NaN to String</title> </head > <body> <p> Convert NaN to String using the toString() Method</p> <p id ="output"></p> <script> let str = NaN; str = str.toString() document.getElementById("output").innerHTML += str +"<br>"; document.getElementById("output").innerHTML += typeof str </script> </body> </html>

Using the || Operator

The OR operator is represented by the || sign in JavaScript. To convert the NaN into a string we use the OR operator with NaN and any String and the OR operator will return that String.

Syntax

NaN || string

Example

In this example, we created a variable num and assigned the value as NaN || “NaN”.

<html> <head> <title> Example: Convert NaN to String</title> </head > <body> <p> Convert NaN to String using OR (||) Operator</p> <p id ="output"></p> <script> let str = NaN || "NaN"; document.getElementById("output").innerHTML += str +"<br>"; document.getElementById("output").innerHTML += typeof str </script> </body> </html>

Using the isNaN() method

The isNaN method is used to check whether a value is NaN or Not. It takes an argument and returns true if the value is NaN and returns false if the value is not NaN. To convert the NaN into Number first check if the variable is NaN or not, if the variable is NaN then assign any number to it.

Syntax

isNaN( string )

Example

In this example, we created variable str and assigned NaN to it after that we checked if the str is NaN using the isNaN method and assigned “NaN” after checking if the condition is true.

<html> <head> <title> Example: Convert NaN to String</title> </head > <body> <p> Convert NaN to String using the isNaN() Method </p> <p id ="output"></p> <script> let str = NaN; if(isNaN(str)){ str = "NaN" } document.getElementById("output").innerHTML += str +"<br>"; document.getElementById("output").innerHTML += typeof str </script> </body> </html>

Using Ternary Operator

The conditional operator or ternary operator first evaluates an expression for a true or false value and then executes one of the two given statements depending upon the result of the evaluation.

Syntax

NaN ? NaN : string

In the given example it is clear how to convert NaN to String using Ternary Operator.

Example

<html> <head> <title> Example: Convert NaN to String</title> </head> <body> <p> Convert NaN to String using Ternary Operator</p> <p id ="output"></p> <script> let str = NaN ? NaN : "NaN"; document.getElementById("output").innerHTML += str +"<br>"; document.getElementById("output").innerHTML += typeof str </script> </body> </html>

Updated on: 11-Aug-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements