
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 Articles
- 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?
- Swift Program to check a given number is finite or not
- Haskell Program to check a given number is finite or not
- Golang Program to check a given number is finite or not
- How is NaN converted to Number in JavaScript?
- Check if input is a number or letter in JavaScript?
- How to check if any value is NaN in a Pandas DataFrame?
- How to use JavaScript to check if a number has a decimal place or it’s a whole number?
- How to Check if a Number is Odd or Even using Python?
- How to check if a string is html or not using JavaScript?
- JavaScript: How to Check if a String is a Literal or an Object?
