
How to check if a value is a number using JavaScript
In JavaScript, there are a lot of ways to check if a value is a number or not. But, in this article, I will try to explain some of these.
We will use the typeof operator and the isNaN() method to check if a value is a number.
Demo : check a variable if number
const myText = 5 ;
if (typeof myText === 'number') {
alert ('Yes! This is a number.');
} else {
alert('Not a number');
}
Try it Yourself »
[1] Check a number using the typeof operator. #
typeof operator returns number if the value is a number. If the value is a string, it returns string.
For example,
- We create a variable and set its value to 5.
const myText = 5
- Then use the typeof operator to check the variable. e.g.
console.log(typeof myText); // output => number
I have use the console.log method to print the output to the console. - In the second example, we will create one more variable and set its value a string. e.g.
const myText = "Our Schools"
- Now, if we check the variable using the typeof operator, it will return string.
console.log(typeof myText); // output => string
typeof operator also returns the following data type.
typeof operator also returns the the following data type. But, in this article, we will ignore them.
Demo: Javascript all data type.
console.log(typeof false);// output => boolean
console.log(typeof [1,2,3,4]);// output => object
console.log(typeof new Date());// output => object
console.log(typeof function() {});// output => function
console.log(typeof null);// output => object
console.log(typeof newVar);// output => undefined *(not assigned)
Try it Yourself »
I hope with the above example I have made you understand the usage of typeof operator.
Now, you have understood that if you want to do something after checking a value, you have to use the if/else statement. For example -
- Let create a variable with its value 5. e.g.
const myText = 5
- In the above example, you have seen if we use the typeof operator to check a variable, it returns string or number. So, we will use the if/else statement to add a condition, if the variable myText is equal to number, we will alert Yes! This is a number., otherwise we will alert Not a number. using the alert() method. e.g.
Demo : check a variable if number #
if (typeof myText === 'number') {
alert('Yes! This is a number.');
} else {
alert('Not a number');
}
Try it Yourself »
[2] Check a value using the isNaN() method. #
We can also use the isNaN() method to check a value is a number or not.
Syntax :
isNaN(value)
The isNaN() method returns true if a value is not a number, otherwise it returns false. e.g.
const myText = "String"; console.log(isNaN(myText)) // output => true
Demo : check value using the isNaN() method. #
const myText = 5;
if (isNaN(myText)){
alert('Not a number');
} else {
alert('Yes! This is a number.');
}
Try it Yourself »
Conclusion #
In this article, you have learned How to check if a value is a number using JavaScript typeof operator and isNaN() method.