isNaN() function with example in JavaScript

JavaScript isNaN() function: Here, we are going to learn about the isNaN() function with example in JavaScript.
Submitted by IncludeHelp, on February 02, 2019

Prerequisite: NaN property in JavaScript

isNaN() function

isNaN() function is a predefined global function in JavaScript and it is used to check whether a given value is an illegible number or not. If the given value is an illegal number - it returns true and if the given value is a legal number – it returns false.

Example:

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
	<script>
		var num1 = 10;
		var num2 = parseInt("Hello");
		
		//printing the values
		document.write("num1: " + num1 + "<br>");
		document.write("num2: " + num2 + "<br>");		
		
		//checking illegal value using isNaN() function
		if(isNaN(num1))
			document.write("num1 is an illegal number <br>");
		else
			document.write("num1 is a legal number <br>");

		if(isNaN(num2))
			document.write("num2 is an illegal number <br>");
		else
			document.write("num2 is a legal number <br>");			
	</script>
</body>
</html>

Output

num1: 10
num2: NaN
num1 is a legal number 
num2 is an illegal number 

JavaScript Built-in Functions »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.