Home » JavaScript

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 





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.