How to Check if a Value is a Number in JavaScript ?
Last Updated : 26 Apr, 2025
To check if a value is a number in JavaScript, use the typeof operator to ensure the value's type is 'number'. Additionally, functions like Number.isFinite() and !isNaN() can verify if a value is a valid, finite number.
Methods to Check if a Value is a Number
There are various ways to check if a value is a number in JavaScript. Below are some common methods:
1. Using the isNaN() method
The isNaN() method is used to check if a value is not a Number. It returns true if the value is NaN (Not-a-Number) and false if the value can be converted into a valid number.
Syntax
isNaN(testingValue);
Now let's understand this with the help of an example:
JavaScript function isNumber(value){ try{ if(isNaN(value)){ throw new Error("Passed value is not a number"); } console.log("Passed value is a number"); } catch(error){ console.log("An error occurred: ", error.message); } } isNumber(55); isNumber("58"); isNumber("GeeksforGeeks");
OutputPassed value is a number Passed value is a number An error occurred: Passed value is not a number
NOTE: The isNaN() method converts the value to a number before checking, which may lead to unexpected results in some cases.
2. Using the typeof operator
The typeof operator can be used to perform the type check of the passed value and compare it with the number type to check whether it is a number or not.
Syntax:
typeof testingValue;
Now let's understand this with the help of example:
JavaScript function isNumber(value){ try{ if(!(typeof value === 'number')){ throw new Error("Passed value is not a number"); } console.log("Passed value is a number"); } catch(error){ console.log("An error occurred: ", error.message); } } isNumber(123); isNumber("123"); isNumber("GFG");
OutputPassed value is a number An error occurred: Passed value is not a number An error occurred: Passed value is not a number
3. Using the Number.isInteger() method
The Number.isInteger() method can also be used to check if a value is number or not. It is mainly used for checking the integer values.
Syntax:
Number.isInteger(testingValue);
Now let's understand this with the help of example:
JavaScript function isNumber(value){ try{ if(!(Number.isInteger(value))){ throw new Error("Passed value is not a number"); } console.log("Passed value is a number"); } catch(error){ console.log("An error occurred: ", error.message); } } isNumber(69); isNumber("231"); isNumber("Geek");
OutputPassed value is a number An error occurred: Passed value is not a number An error occurred: Passed value is not a number
4. Using the Number.isFinite() method
The Number.isFinite() method is used to check if the passed value is finite or not. If it unables to parse the value it returns false and if it parses the value then it check if it is finit or infinite.
Syntax:
Number.isFinite(testingValue);
Now let's understand this with the help of example:
JavaScript function isNumber(value){ try{ if(!(Number.isFinite(value))){ throw new Error("Passed value is not a number"); } console.log("Passed value is a number"); } catch(error){ console.log("An error occurred: ", error.message); } } isNumber(108); isNumber("11"); isNumber("Hello");
OutputPassed value is a number An error occurred: Passed value is not a number An error occurred: Passed value is not a number
Why Checking if a Value is a Number is Important?
In JavaScript, not all values are of the "Number" type, and you might encounter:
- Strings that represent numbers (e.g., "123").
- NaN (Not-a-Number), which is a special value in JavaScript.
- Other data types like objects, arrays, or undefined that may need validation before performing number-specific operations.
Conclusion
In JavaScript, checking if a value is a number is important for tasks like user input validation and calculations. Methods such as isNaN(), typeof, Number.isInteger(), parseInt(), and the unary plus (+) operator help easily determine if a value is a valid number, ensuring smooth data handling and avoiding errors.
Similar Reads
How to check if a value is object-like in JavaScript ?
In JavaScript, objects are a collection of related data. It is also a container for name-value pairs. In JavaScript, we can check the type of value in many ways. Basically, we check if a value is object-like using typeof, instanceof, constructor, and Object.prototype.toString.call(k). All of the ope
4 min read
How to check if a Variable Is Not Null in JavaScript ?
In JavaScript, checking if a variable is not null ensures that the variable has been assigned a value and is not empty or uninitialized. This check is important for avoiding errors when accessing or manipulating data, ensuring that the variable holds valid, usable content before proceeding with oper
4 min read
How to Check if a Variable is an Array in JavaScript?
To check if a variable is an array, we can use the JavaScript isArray() method. It is a very basic method to check a variable is an array or not. Checking if a variable is an array in JavaScript is essential for accurately handling data, as arrays have unique behaviors and methods. Using JavaScript
2 min read
How to check if the value is primitive or not in JavaScript ?
To check if the value is primitive we have to compare the value data type. As Object is the non-primitive data type in JavaScript we can compare the value type to object and get the required results. Primitive data types are basic building blocks like numbers and characters, while non-primitive data
3 min read
How to check for null values in JavaScript ?
The null values show the non-appearance of any object value. It is usually set on purpose to indicate that a variable has been declared but not yet assigned any value. This contrasts null from the similar primitive value undefined, which is an unintentional absence of any object value. That is becau
4 min read
How to Check if Object is JSON in JavaScript ?
JSON is used to store and exchange data in a structured format. To check if an object is JSON in JavaScript, you can use various approaches and methods. There are several possible approaches to check if the object is JSON in JavaScript which are as follows: Table of Content Using Constructor Type Ch
3 min read
JavaScript Program to Check if a Number is Float or Integer
In this article, we will see how to check whether a number is a float or an integer in JavaScript. A float is a number with a decimal point, while an integer is a whole number or a natural number without having a decimal point. Table of ContentUsing the Number.isInteger() MethodUsing the Modulus Ope
2 min read
How to Validate Decimal Numbers in JavaScript ?
Validating user input is an essential aspect of Web Development. As a developer, when we are playing with the numeric inputs provided by the end-user, it is quite important to ensure that the input provided by the user is in the correct format. We can use the regular expression to Validate Decimal N
2 min read
How to check whether a number is NaN or finite in JavaScript ?
When working with numbers in JavaScript, it's important to know how to determine if a value is NaN (Not-a-Number) or finite. This knowledge is crucial for data validation, error handling, and ensuring your code behaves as expected. In this article, we will see how to check whether the number is NaN
3 min read
How to check if a number evaluates to Infinity using JavaScript ?
The task is to check whether the number evaluates to infinity or not with the help of JavaScript. Here are a few techniques discussed. Approach 1: Checking if the number is equal to the Number.POSITIVE_INFINITY or Number.NEGATIVE_INFINITY . Example: This example uses the approach discussed above. C/
2 min read