How to check if a string is a number in JavaScript

In JavaScript, distinguishing between strings and numbers can often be crucial. This post delves into various methods to check if a given string is indeed a number.

Using the isNaN function

The most straightforward approach to determine if a string is a number in JavaScript is by using the built-in isNaN (stands for "is Not a Number") function.

function isNumber(value) { return !isNaN(value); } console.log(isNumber("123")); // true console.log(isNumber("abc")); // false console.log(isNumber("1.23")); // true

However, there's a caveat. isNaN will return false for some non-numeric values, such as null or an empty string. So, it's not a foolproof method.

Using a regular expression

A more robust way is to use a regular expression that matches valid number patterns.

function isNumber(value) { return /^-?\\d*(\\.\\d+)?$/.test(value); } console.log(isNumber("123")); // true console.log(isNumber("-123")); // true console.log(isNumber("1.23")); // true console.log(isNumber("abc")); // false console.log(isNumber("")); // false

This method is more accurate than isNaN, as it specifically matches strings that are valid representations of numbers.

Using parseFloat and parseInt

JavaScript provides functions like parseFloat and parseInt to convert strings into numbers. If the string isn't a valid number, they will return NaN.

function isNumber(value) { return !isNaN(parseFloat(value)) && isFinite(value); } console.log(isNumber("123")); // true console.log(isNumber("1.23")); // true console.log(isNumber("abc")); // false

parseFloat will accept any string that starts with a number and ignores non-numeric characters that follow. If strict validation is needed, it's better to use a combination of methods.

Edge cases with leading/trailing spaces

Often, input strings might come with leading or trailing spaces. Depending on your needs, you might handle them by trimming the string or by modifying the regular expression.

console.log(isNumber(" 123 ")); // Depending on the method, this might return false

Using the Number function

Another approach to determine if a string is a number is using the built-in Number function. If the string isn't a valid number representation, it will return NaN.

function isNumber(value) { return !isNaN(Number(value)); } console.log(isNumber("123")); // true console.log(isNumber("1.23")); // true console.log(isNumber("abc")); // false

Checking for hexadecimal, octal, and binary

JavaScript supports hexadecimal (prefixed with 0x), octal (prefixed with 0o), and binary (prefixed with 0b) literals. Depending on your requirements, you might consider these as valid numbers or not.

console.log(isNumber("0xff")); // Hexadecimal for 255

Considerations when working with large numbers

When dealing with large numbers, be aware of the limitations of JavaScript's Number type.

console.log(Number("12345678901234567890")); // 12345678901234568000

For very large or precise numbers, consider using libraries like BigInt or third-party solutions such as bignumber.js for more precision.

Handling Infinity

The functions parseFloat and Number can return Infinity for very large numbers. You might want to treat this separately based on your use-case.

console.log(isNumber("1e308")); // Returns Infinity

In summary

Checking if a string is a number in JavaScript can be approached in various ways, each with its strengths and weaknesses. Whether you prefer a simple method like isNaN or a more rigorous approach using regular expressions, always be conscious of edge cases and the inherent limitations of JavaScript's number handling.

Invite only

We're building the next generation of data visualization.