How to check whether a NaN is a NaN or not in JavaScript?

Front End TechnologyJavascriptObject Oriented Programming

In this article let us understand how to check whether a NaN is a NaN or not in JavaScript. Literal constant that is not quoted Not-a-Number is represented by NaN, a special value. NaN is commonly used to signal an error condition for a function that should return a valid number since it always compares unequally to any number, including NaN.

Differentiating between different undefined values in JavaScript can be tricky. When working with NaN values in Boolean operations, there are a handful of difficulties to be careful of.

The numeric type in JavaScript permits you to describe numbers of any type, including integers and floating-point values. A specific value for a JavaScript number is NaN, which stands for Not-a-Number.

Syntax

Following is the syntax of NaN method

var val = Number.NaN

NaN is returned by five different sorts of operations. As a result of a failed procedure on numbers, JavaScript uses NaN.

  • The number cannot be decoded

  • The outcome of a math operation that isn't a real number

  • An argument's value is NaN.

  • Form that is indefinite

  • Any operation using a string that isn't an addition operation

Example 1

JavaScript returns NaN if it can't convert a string to a number. NaN shows that the parsing has failed in this circumstance. Consider the following scenario −

<!DOCTYPE html> <html> <title>How to check whether a NaN is a NaN or not in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <script> const tutpoint = 'tuttorialspoint555'; const number = parseInt(tutpoint); document.write(number); </script> </body> </html>

Example 2

In this example we will understand: Undefined is the primitive value represented by the global undefined attribute. It's a basic type in JavaScript.

<!DOCTYPE html> <html> <title>How to check whether a NaN is a NaN or not in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <script> function tutpoint(result) { if (result === undefined) { return 'This gives you undefined value!'; } return result; } let demo; document.write(tutpoint(demo)); </script> </body> </html>

Example 3

As an argument, NaN is used, when an expression contains the character NaN, it will always return NaN. Consider the following scenario −

<!DOCTYPE html> <html> <title>How to check whether a NaN is a NaN or not in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <script> const tutpoint = 55 + 5 / NaN; document.write(tutpoint); </script> </body> </html>

Example 4

Making use of undefined types, when performing an arithmetic operation in the indeterminate form, the result is NaN. Consider the following scenario −

<!DOCTYPE html> <html> <title>How to check whether a NaN is a NaN or not in JavaScript - TutorialsPoint</title> <head> <charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <script> const tutpoint = 55 + 0 / 0; document.write(tutpoint); </script> </body> </html>

Example 5

The Math.sqrt() function returns the square root of an integer in this example. Passing -2 results in NaN because it only takes non-negative numbers. Consider the following scenario −

<!DOCTYPE html> <html> <title>How to check whether a NaN is a NaN or not in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <script> const tutpoint = Math.sqrt(-2); document.write(tutpoint); </script> </body> </html>

isNaN()

The isNaN() method examines a value to see if it is NaN. The standard library method isNaN is one of the most useful methods for performing this check (). You may choose to use Number.isNaN instead of the isNaN function because the forcing can be unexpected ().

Call the Number.isNaN() function with the number as a parameter to determine if a number is NaN. If the passed-in value is NaN and has a number type, the Number.isNaN method returns true; else, it returns false.

Syntax

Following is the syntax of isNaN() method

isNaN(value)

Sr.No Parameter & Description
1

value

The value to be tested

Example 1

In this example let us understand how to identifying whether or not a value is NaN. isNaN() is a global method in JavaScript which returns true if its argument is NaN −

<!DOCTYPE html> <html> <title>How to check whether a NaN is a NaN or not in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <script> const tutpoint = 10 + 0 / 0; document.write(isNaN(tutpoint)); </script> </body> </html>

Example 2

In this example let us understand how to identifying whether or not a value is NaN. isNaN() is a global method in JavaScript which returns true if its argument is NaN −

<!DOCTYPE html> <html> <title>How to check whether a NaN is a NaN or not in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <div id="result"></div> <script> let tutpoint = "Welcome to tutorialspoint!"; document.getElementById("result").innerHTML = isNaN(tutpoint); </script> </body> </html>

Example 3

In this example let us understand the Number.isNaN() Method which returns false.

<!DOCTYPE html> <html> <title>How to check whether a NaN is a NaN or not in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <div id="result"></div> <script> let tutpoint = "Welcome to tutorialspoint!"; document.getElementById("result").innerHTML = Number.isNaN(tutpoint); </script> </body> </html>
raja
Updated on 04-Aug-2022 11:50:13

Advertisements