How to check if string is number in JavaScript? [SOLVED]


Written By - Olorunfemi Akinlua
Advertisement

Within our applications knowing the actual data type of the data we have is important, and being able to manipulate them properly is essential. One area we need it in is with numbers and strings. In JavaScript, there are various ways to check if a string is a number.

In this article, we will discuss three ways to check if a string is a number.

 

Method-1: Use the isNaN method to check if a string is a number

The most common way to check if a string is a number is to use the typeof operator. This operator returns the type of a variable, which is either string, number, boolean, undefined, or object. If the typeof operator returns number, then the variable is definitely a number. However, if the typeof operator returns string, then the string may or may not be a number. In order to check if a string is definitely not a number, you can use the isNaN() function. This function returns true if the value is not a number, and false if the value is a number.

let str = "      20";
let strJava = "javascript 20";

function isNumber(value) {
    if (typeof value === "string") {
        return !isNaN(value);
    }
}

console.log(isNumber(str));
console.log(isNumber(strJava));

Output

true
false

As we can see it returns false when the string contained the number 20 regardless of the whitespace.

 

Method-2: Use the + operator to check if a string is a number

We can make use of the unary operator - + - which helps convert a string to a number. Using the same example as in the previous section, we can check if the string is a number.

let str = "      20";
let strJava = "javascript 20";

function isNumber(value) {
    const conv = +value;
    if (conv) {
        return true;
    } else {
        return false;
    }
}

console.log(isNumber(str));
console.log(isNumber(strJava));

Output

true
false

 

Method-3: Use regex to check if a string is a number

Regular expression provides a way to check for number patterns within strings using the /\\d/ pattern and the test method which checks if the pattern exists within the string. A little modification to the regex pattern allows us to check if a string is a number.

Advertisement
let str = "20.2";
let strJava = "javascript 20";

function isNumber(value) {
    return /^-?\\d/.test(value);
}

console.log(isNumber(str));
console.log(isNumber(strJava));

Output

true
false

 

Summary

We have provided three approaches to check if a string is a number; the isNaN method, the unary + operator, and regex. All of these approaches can be tweaked and improved depending on use cases.

References

Expressions and operators - JavaScript | MDN (mozilla.org)
isNaN() - JavaScript | MDN (mozilla.org)
Regular expressions - JavaScript | MDN (mozilla.org)

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment