

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript: How to check if a number is NaN or finite?
Often we need to check whether a finite number or undefined returns the same to the user. In this article also we will be checking if a number is actually a finite number or Not a Number, i.e., NaN.
Below is the method to check whether a number is finite or not.
isNaN()
This is a method provided by JavaScript to check whether a number is a finite number or not. If this method returns false, then the number is a finite number else the number is not finite.
Syntax
isNaN(number )
Example 1
In the below example, we are going to use the above method to check if the number is finite or not. The above method will either return true or false and based upon that we can decide if the number is finite or not.
# index.html
<!DOCTYPE html> <html> <head> <title>Checking If a Number is Even</title> </head> <body> <h1 style="color: green;"> Welcome To Tutorials Point </h1> <script> function isFinite(x) { if(isNaN(x)) { return 'It is NaN'; } else { return 'It is a Finite Number'; } } console.log(isFinite(21)); console.log(isFinite('Tutorials Point')); </script> </body> </html>
Output
You will get output in the console similar to the below screenshotisFinite −
isFinite()
Similar to isNaN(), we have a isFinite() method that basically checks whether a number is finite or not. If the number is finite it will return true else false.
Syntax
isFinite(number )
Example 2
In the below example, we are checking if the number is a finite number or not.
# index.html
<!DOCTYPE html> <html> <head> <title>Checking If a Number is Even</title> </head> <body> <h1 style="color: green;"> Welcome To Tutorials Point </h1> <script> function isFinite(x) { if(isNaN(x)) { return 'It is NaN'; } else { return 'It is a Finite Number'; } } console.log(isFinite('2C')); console.log(isFinite(01010101)); </script> </body> </html>
Output
You will get output in the console similar to the below screenshot −
- Related Questions & Answers
- How to check whether a number is finite or not in JavaScript?
- How to check whether a NaN is a NaN or not in JavaScript?
- How to check if a variable is NaN in JavaScript?
- Java Program to check if a Float is Infinite or Not a Number(NAN)
- How do you check that a number is NaN in JavaScript?
- Check if input is a number or letter in JavaScript?
- How is NaN converted to Number in JavaScript?
- How to check if any value is NaN in a Pandas DataFrame?
- How to Check if a Number is Odd or Even using Python?
- Check if a number is a Krishnamurthy Number or not in C++
- C# Program to check if a number is prime or not
- Python program to check if a number is Prime or not
- PHP program to check if a number is prime or not
- Checking if a double (or float) is NaN in C++
- JavaScript: How to Check if a String is a Literal or an Object?