Skip to content
DeveloperMemos

Checking If A String is a Number in JavaScript

JavaScript, String, Number2 min read

When working with data input or validation in JavaScript, it's often necessary to check if a given string is a valid number. Whether you're building form validations, mathematical calculations, or data processing applications, having the ability to verify if a string represents a numeric value can be crucial. In this article, we'll discuss different techniques you can use to achieve this goal.

Let's dive into the methods and examples that will allow you to determine whether a given string is a number in JavaScript.

Using the isNaN Function

One of the simplest ways to check if a string is a number in JavaScript is by leveraging the isNaN function. The isNaN function returns true if the provided value cannot be converted into a valid number. Here's an example:

1const string1 = '42';
2const string2 = 'hello';
3
4console.log(isNaN(string1)); // Output: false
5console.log(isNaN(string2)); // Output: true

In the above example, the isNaN function returns false for the string1, indicating that it can be successfully parsed as a number. On the other hand, string2 triggers true since it cannot be converted to a valid numeric value.

Regular Expressions Approach

Another approach to check if a string is a number involves using regular expressions. Regular expressions provide powerful pattern matching capabilities, and they can be utilized to validate numeric strings. Let's take a look at an example:

1const numericString = '12345';
2const nonNumericString = 'hello';
3
4console.log(/^\d+$/.test(numericString)); // Output: true
5console.log(/^\d+$/.test(nonNumericString)); // Output: false

In the above code snippet, we use the regular expression /^\d+$/ to match strings that consist of one or more digits. By applying the test method on the regular expression object, we can determine if the provided string matches the numeric pattern.

Using the typeof Operator

JavaScript provides the typeof operator, which allows us to obtain the type of a given value or variable. By utilizing this operator in combination with a conditional statement, we can verify if a string represents a number. Here's an example:

1function isNumber(value) {
2 return typeof value === 'number' && !isNaN(value);
3}
4
5console.log(isNumber(42)); // Output: true
6console.log(isNumber('hello')); // Output: false

In the above code snippet, we define a function isNumber that checks if the provided value has a type of 'number' and is not equal to NaN. This approach provides more flexibility as it can handle both literal numbers and variables containing numeric values.

Using the Number Function

The Number function in JavaScript can be used to explicitly convert a value into a numeric representation. If the conversion fails, it returns NaN. We can utilize this behavior to determine if a string can be interpreted as a number. See the following example:

1const string1 = '42';
2const string2 = 'hello';
3
4console.log(!isNaN(Number(string1))); // Output: true
5console.log(!isNaN(Number(string2))); // Output: false

In the above code snippet, we pass the strings to be checked as arguments to the Number function. By applying isNaN to the result and negating it with !, we obtain a boolean value indicating whether the string represents a number.