. Advertisement .
..3..
. Advertisement .
..4..
There are several ways to check if string is a number in JavaScript. This guide will show you how to make use of them in your program with examples.
Check If String Is A Number In JavaScript
With isNaN()
In JavaScript, there is a property of the global object named NaN. It presents Not-A-Number and has a global scope. Not-A-Number is its initial value, just like the Number.NaN
property.
Modern browsers don’t allow you or JavaScript to configure or write to this property. Even if you can, don’t override it to avoid unexpected behaviors.
It is typically generated by arithmetic operations when they produce unrepresentable or undefined values. The existence of a NaN doesn’t necessarily mean overflow conditions. There are plenty of situations in which JavaScript may return this property.:
- when it can’t parse a number,
- when the expression has an indeterminate form,
- when an argument’s operand is NaN,
- when the result of a math operation isn’t a real number,
- when the operation involves a string but isn’t an addition operation.
JavaScript developers rarely need to use NaN
in their programs. However, coming with this property is the isNan()
function, which determines if a value is NaN.
It is important to note that NaN values are also returned when JavaScript attempts to convert non-numeric values to numeric values with no primitive numeric value available. You can take advantage of this to check if a string is numeric in JavaScript.
Its syntax is very simple and doesn’t require any argument other than the value you want to check:
isNaN(value)
The function will return True if value isn’t a number and False otherwise.
This is how you can put the isNaN()
function in action and perform the check on various strings:
function check(x) {
if (isNaN(x)) {
return 'Not a Number!';
} else {
return 'A Number';
}
}
console.log(check('4'));
console.log(check('3.14'));
console.log(check('123abc'));
console.log(check('ITTutoria'));
Output:
A Number
A Number
Not a Number!
Not a Number!
As you can see, the isNaN()
function returns False for both ‘4’ and ‘3.14’ strings, which means they can be converted to numbers (an int and a float). This isn’t the case with the other two strings. The final one doesn’t contain any numeric characters, while the third string has letters after the first three numbers (which aren’t valid numbers).
With Number()
The Number()
constructor creates a new Number value from an argument, such as a string. If you use the new keyword, it returns a Number object, which is different (it isn’t a primitive).
The constructor will return NaN when it can’t convert the value to the Number type. You can use this to make a function to check whether a string is numeric or not:
console.log(Number('4'));
console.log(Number('3.14'));
console.log(Number('123abc'));
console.log(Number('ITTutoria'));
Output:
4
3.14
NaN
NaN
The first two checks return two valid numbers, indicating their original strings are numeric. On the other hand, the final two strings, as expected, return NaN.
Check Against NaN
Unlike other values, you can’t use the equality operators (===
and ==
) to compare your string to NaN to determine whether it is NaN. Both NaN === NaN
and NaN == NaN
expressions both evaluate to false, which isn’t what we are expecting.
If you insist on doing this, you will get the wrong results:
function check(x) {
if (x === NaN) {
return 'Not a Number!';
} else {
return 'A Number';
}
}
console.log(check('4'));
console.log(check('3.14'));
console.log(check('123abc'));
console.log(check('ITTutoria'));
Output:
A Number
A Number
A Number
A Number
Summary
You can check if string is a number in JavaScript with isNaN()
or Number()
. They also rely on the NaN (Not-A-Number) to represent the result. If you want to convert a string to a number, check out this guide.
Leave a comment