JavaScript NaN Function

The number functionality in JavaScript allows for the representation of both integer and floating-point values. A specific value within the JavaScript number type is referred to as NaN, which stands for Not-a-Number.

NaN is an attribute of the global javascript object. The global object is used to show the window object in web browsers. There are used to NaN function in different ways.

  • The javascript shows NaN value in windows in browsers. window.NaN
  • The javascript shows the NaN value as a global object in Node.js. global.NaN
  • The NaN has the typeof number as shown in the following code: console.log(typeof NaN); We can assign a variable to NaN in two ways. It's a non-number value. It checks if a number is legitimate.
  • var value = NaN
  • Syntax

JavaScript offers a global function known as isNaN. This function yields true if the argument passed to it is determined to be NaN as a result of an operation.

Example

isNaN(valueOfOperation)

The NaN function displays a boolean result, indicating either true or false, based on the provided syntax.

Example

const output = 50 + 0 / 0;

console.log(isNaN(output));

Examples

The subsequent example illustrates various forms of numeric values as well as NaN (Not-a-Number) in JavaScript.

Example 1:

The following example illustrates the representation of NaN in JavaScript by utilizing various display values. In this instance, we incorporate positive numbers, negative numbers, and straightforward mathematical expressions.

Example

<!DOCTYPE html>

<html>

<body>

<h1>JavaScript isNaN() Methods</h1>

<p> Javascript isNaN() function returns true if a value is NaN. </p>

<p id="demovalue"></p>

<script>

let result =

"Is '152' NaN? " + isNaN('152') + "<br>" +

"Is '-1.52' NaN? " + isNaN('-1.52') + "<br>" +

"Is '0-0' NaN? " + isNaN('0-0') + "<br>" +

"Is '15-2' NaN? " + isNaN('15-2') + "<br>" +

"Is 15-2 NaN? " + isNaN(15-2) + "<br>" +

"Is 0 NaN? " + isNaN(0)+ "<br>" +

"Is 00 NaN? " + isNaN('00');

document.getElementById("demovalue").innerHTML = result;

</script>								

</body>

</html>

Output

You can observe the Boolean format data to verify whether the input value is NaN.

Example 2: This illustration demonstrates the representation of NaN (Not a Number) in JavaScript through various display formats. In this case, we utilize inputs in the form of a number, a string, a mathematical equation, and a date format.

Example

<!DOCTYPE html>

<html>

<body>

<h1>JavaScript isNaN() Methods</h1>

<p> Javascript isNaN() function returns true if a value is NaN. </p>

<p id="demovalue"></p>

<script>

let result =

"Is '152' NaN? " + isNaN('152') + "<br>" +

"Is '-1.52+11*2' NaN? " + isNaN('-1.52+11*2') + "<br>" +

"Is -1.52+11*2 NaN? " + isNaN(-1.52+11*2) + "<br>" +

"Is 'Example' NaN? " + isNaN('Example') + "<br>" +

"Is 2022/12/12 NaN? " + isNaN(2022/12/12) + "<br>" +

"Is '2022/12/12' NaN? " + isNaN('2022/12/12') + "<br>" +

"Is 0+0-0/0 NaN? " + isNaN(0+0-0/0)+ "<br>" +

"Is '0+0-0/0' NaN? " + isNaN('0+0-0/0');

document.getElementById("demovalue").innerHTML = result;

</script>

</body>

</html>

Output

The Boolean format data can be utilized to verify whether the input value is NaN.

Example 3:

The following example demonstrates the representation of NaN values in JavaScript through various display formats. It incorporates both positive and negative numbers, along with basic arithmetic expressions. Additionally, there are alternative approaches available to verify whether a specific number is classified as NaN or not.

Example

<!DOCTYPE html>

<html>

<body>

<h1> JavaScript isNaN() Methods </h1>

<p> Javascript isNaN() function returns true if a value is NaN. </p>

<p id="demovalue"></p>

<script>

var val1 = 52+11-10;

var val2 = '52+11-10';

var val3 = 11-11*0;

var val4 = '11-11*0';

let result =

"Is val1 NaN? " + isNaN(val1) + "<br>" +

"Is val2 NaN? " + isNaN(val2) + "<br>" +

"Is val3 NaN? " + isNaN(val3) + "<br>" +

"Is val4 NaN? " + isNaN(val4);

document.getElementById("demovalue").innerHTML = result;

</script>

</body>

</html>

Output

You can observe the Boolean format data to verify whether the input value is NaN.

Example 4:

The example provided illustrates the representation of NaN (Not-a-Number) in JavaScript by utilizing various display values. In this context, we employ additional functions including parseInt and parseFloat.

Example

<!DOCTYPE html>

<html>

<body>

<h1>JavaScript isNaN() Methods</h1>

<p> Javascript isNaN() function returns true if a value is NaN. </p>

<p id="demovalue"></p>

<script>

let result =

"Is '152' NaN? " + isNaN(parseInt('152', 10)) + "<br>" +

"Is '15.2' NaN? " + isNaN(parseInt('15.2', 10)) + "<br>" +

"Is '-152' NaN? " + isNaN(parseInt('-152', 10)) + "<br>" +

"Is '-152' NaN? " + isNaN(parseInt('', 10)) + "<br>" +

"Is 'Example' NaN? " + isNaN(parseInt('Example', 10)) + "<br>" +

"Is 'new Date()' NaN? " + isNaN(parseInt('new Date()', 10));

document.getElementById("demovalue").innerHTML = result;

</script>

</body>

</html>

Output

You can observe the Boolean format data to verify whether the input value is NaN.

Example 5:

The example provided illustrates the occurrence of NaN (Not a Number) within a JavaScript expression, along with various functions that utilize distinct output values.

Example

<!DOCTYPE html>

<html>

<body>

<h1>JavaScript isNaN() Methods</h1>

<p> Javascript isNaN() function returns true if a value is NaN. </p>

<p id="demovalue"></p>

<script>

let result =

"Is ' ' " + isNaN(' ') + "<br>" +

"Is '' " + isNaN('') + "<br>" +

"Is 'NaN' value  NaN? " + isNaN('NaN') + "<br>" +

"Is NaN value NaN? " + isNaN(NaN) + "<br>" +

"Is empty NaN? " + isNaN() + "<br>" +

"Is 'parseInt' NaN? " + isNaN(parseInt('', 10)); 

document.getElementById("demovalue").innerHTML = result;

</script>

</body>

</html>

Output

You can observe the Boolean formatted data to verify whether the input value is NaN (Not a Number).

Why use the NaN Function

JavaScript NaN function shows the result of a failed operation on numbers, including:

  • It is used for Parsing numbers.
  • Javascript is used to undefine as an operand for operation.
  • Javascript is used NaN function as an operand
  • Javascript is used to indeterminate forms
  • Javascript is used for Passing invalid arguments for math functions.
  • Supported Browser

The following browser is used and supports the javascript NaN function. Almost all browsers and the latest version are supported by the NaN function.

  • Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 4 and above
  • Opera 3 and above
  • safari 1 and above
  • Conclusion

The NaN function serves the purpose of determining whether a specified value is NaN in JavaScript. This function is globally accessible and can handle all data types.

Input Required

This code uses input(). Please provide values below: