How to Check if a String is a Number in JavaScript

In JavaScript, there are times when we need to determine whether a string represents a number. This article will introduce three methods to achieve this: using the built-in isNaN() function, using regular expressions, and utilizing the ternary operator.

Home > Blog > How to Check if a String is a Number in JavaScript

In JavaScript, there are times when we need to determine whether a string represents a number. This is particularly common when dealing with user input or data validation. This article will introduce three methods to achieve this: using the built-in isNaN() function, using regular expressions, and utilizing the ternary operator.

1.Using the isNaN() Function

The isNaN() function is used to check whether the passed parameter is not a number. This function's behavior can be somewhat confusing because it not only checks if a string is a number but also examines other cases. Therefore, we need to combine it with the parseFloat() function to ensure that the parameter is parsed as a number.Below is an example code using the isNaN() method:

function isNumeric(str) {
  return !isNaN(str) && !isNaN(parseFloat(str));
}

console.log(isNumeric('123')); // Output: true
console.log(isNumeric('abc')); // Output: false
Check if a String is a Number in JavaScript - isNaN()

2.Using Regular Expressions

Another method to check if a string is a number is by using regular expressions. Regular expressions are powerful pattern matching tools that allow for more flexible string handling.Below is an example code using regular expressions:

function isNumeric(str) {
  return /^\d+$/.test(str);
}

console.log(isNumeric('123')); // Output: true
console.log(isNumeric('abc')); // Output: false
Check if a String is a Number in JavaScript - Regular Expressions

3.Using the Ternary Operator

The ternary operator is a conditional statement in JavaScript that returns different values based on a condition's truth or falsity. We can leverage the ternary operator to check if a string is a number.Below is an example code using the ternary operator:

function isNumeric(str) {
  return !isNaN(parseFloat(str)) && isFinite(str) ? true : false;
}

console.log(isNumeric('123')); // Output: true
console.log(isNumeric('abc')); // Output: false
Check if a String is a Number in JavaScript - Ternary Operator

Conclusion

In this article, we have introduced three methods to check whether a string represents a number in JavaScript. Using the isNaN() function is a simple method, but it's important to be aware that its behavior may lead to unexpected results. Another approach is to use regular expressions, which provide more flexibility in pattern matching. Lastly, we explored using the ternary operator as a concise way to check if a string is a number. Depending on your needs and preferences, you can choose any of these methods to implement string-to-number checks. Regardless of the method chosen, you can effectively verify whether a string is a number to meet your programming requirements.

How to Check if a String is a Number in JavaScript
In JavaScript, there are times when we need to determine whether a string represents a number. This article will introduce three methods to achieve this: using the built-in isNaN() function, using regular expressions, and utilizing the ternary operator.

Learn more: