Verify if a string represents a valid number in JavaScript
This post will discuss how to verify if a string represents a valid number in JavaScript.
There are several methods to determine if a string represents a valid number in JavaScript. Here are some of the most common functions:
1. Using isNaN() function
One way is to use the isNaN() function, which is a global function that checks if a value is not a number. This function returns true if the value is not a number, and false otherwise. We can use this function to check if a string is a valid number by passing it as an argument, and negating the result. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
function isNumeric(str) { return !isNaN(str); } console.log(isNumeric("100")); // true console.log(isNumeric("5.5")); // true console.log(isNumeric("0.0752E+2")); // true console.log(isNumeric("Infinity")); // true console.log(isNumeric("-Infinity")); // true console.log(isNumeric("0x64")); // true console.log(isNumeric("")); // true console.log(isNumeric("true")); // false console.log(isNumeric("[]")); // false console.log(isNumeric("NaN")); // false console.log(isNumeric("Hello")); // false console.log(isNumeric("3.14f")); // false |
This method will return true if the string can be converted to a number, and false otherwise. However, it may also return true for some values that are not strictly numbers, such as empty strings.
2. Using Number() function
Another way is to use the Number() function, which converts any value to a number. This method will return a numeric value if the value can be converted to a number, and NaN (not a number) otherwise. We can use this function to check if a string is a valid number by passing it as an argument, and using the isFinite() function to check if the result is finite. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
function isNumeric(str) { return isFinite(Number(str)); } console.log(isNumeric("100")); // true console.log(isNumeric("5.5")); // true console.log(isNumeric("0.0752E+2")); // true console.log(isNumeric("")); // true console.log(isNumeric("0x64")); // true console.log(isNumeric("true")); // false console.log(isNumeric("[]")); // false console.log(isNumeric("Infinity")); // false console.log(isNumeric("-Infinity")); // false console.log(isNumeric("NaN")); // false console.log(isNumeric("Hello")); // false console.log(isNumeric("3.14f")); // false |
This method will return true if the string is a valid number, and false otherwise. However, it may also return false for some values that are technically numbers, such as Infinity or -Infinity.
3. Using a regular expression
A third way is to use a regular expression, which is a pattern matches strings or parts of strings. We can use a regular expression to check if a string matches the format of a valid number, such as an optional sign, one or more digits, an optional decimal point, and optional exponential notation. To use a regular expression in JavaScript, we can use the RegExp.test() function. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
function isNumeric(str) { let regex = /^[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?$/; return regex.test(str); } console.log(isNumeric("100")); // true console.log(isNumeric("5.5")); // true console.log(isNumeric("0.0752E+2")); // true console.log(isNumeric("0x64")); // false console.log(isNumeric("")); // false console.log(isNumeric("true")); // false console.log(isNumeric("[]")); // false console.log(isNumeric("Infinity")); // false console.log(isNumeric("-Infinity")); // false console.log(isNumeric("NaN")); // false console.log(isNumeric("Hello")); // false console.log(isNumeric("3.14f")); // false |
This method will return true if the string matches the regular expression, and false otherwise. However, it may also return false for some values that are valid numbers in JavaScript, such as hexadecimal literals.
That’s all about verifying if a string represents a valid number in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)