Check if a String is a Number in JavaScript: 3 Effective approach.

0

This blog post will guide you through various methods to check if a string in JavaScript represents a number. In simpler terms, we'll explore ways to determine if a string contains only numeric characters. We'll delve into different approaches to achieve this task.


check if string is a number


JavaScript Methods Used:

isNaN().

Match().

Split().


And we also used a loop in combination with the match method.


Approach 1: isNaN Method.

The isNaN (is Not a Number) method returns true if a value is not a number. We'll employ this method to determine whether a given string consists exclusively of numerical characters or not. See below example code.


JavaScript


    let string = "12345 6789";
    console.log("String: ", string);
    
    if (!isNaN(string)) {
        console.log("Output: ", "String is number.");
    } else {
        console.log("Output: ", "String is not a number.");
    }
  

Output:


string is number


As you can see I’ve just added one single space between the numbers string. And I get the output that the string is not a number. So for that we need to replace the spaces with no space and then check if the string is a number or not. 

See below code,


JavaScript


    let string = "12345 6789";
    console.log("String: ", string);
    let new_string = string.trim().replace(/[\s+]/g , "");
    
    if (!isNaN(new_string)) {
        console.log("Output: ", "String is number.");
    } else {
        console.log("Output: ", "String is not a number.");
    }
  

Output:


string is a number


Approach 2: Match Method.

We will use the match method with regular expressions to check if a string contains only digits (0-9). If the string contains any characters other than digits, it is not a number; otherwise, it is a number.


JavaScript


    let string = "Hello welcome 2 maketechstuff.com";
    console.log("string: ", string);

    if (string.match(/[^0-9]/g)) {
        console.log("Output: ", "String is not a number.");
    } else {
        console.log("Output: ", "String is number.");
    }

  

Output:


string is not a number


Approach 3: Combination of For loop, split method and match method.

  1. First, we'll divide the string into an array of characters using the split method.
  2. Next, we'll create a variable named "n" and initialise it with the value 0.
  3. After converting the string into an array, we'll iterate through each character. For each character, we'll use a regular expression and the match method to determine whether it's a digit. If it's not a digit, we'll increment the value of the variable "n" by 1.
  4. Finally, we'll check the value of the variable "n". If it's 0, it indicates that the string consists entirely of digits and is therefore a number. If "n" is not 0, it means the string contains non-digit characters and is not a number.

JavaScript


    let string = "123456789";
    console.log("string: ", string);
    let array = string.split("");
    let n = 0;
    for (let i = 0; i < array.length; i++) {
        const element = array[i];
        if (!element.match(/[0-9]/)) {
            n++;
        }
    }
    if (n == 0) {
        console.log("Output: ", "String is a number.");
    } else {
        console.log("Output: ", "String is not a number.");
    }

  

Output:


string is a number


This approach 2 and 3 also gives output that the string is not a number, when the whole string has a numeric value with space. So as I said you need to replace the string spaces with no space and then check for the string is a number or not a number.


So that's how you can check if the string is a full number or not using simple JavaScript. Feel free to write suggestions and questions in the comment section.


You may like:

  1. How to split the string to array of characters.
  2. How to split the string to an array of words.
  3. Replace the multiple spaces present in the string with a single space.
  4. Check if string contains a special characters or characters other then alphanumeric values, space and dot.
  5. How to remove the numeric values present in the string using JavaScript.
  6. How to detect the numeric values present in the string using JavaScript.
  7. Extract the numeric values from a string.
  8. Count the words from a string.
  9. Count the characters from a string.
Tags

Post a Comment

0Comments

Share your thoughts.

Post a Comment (0)
To Top