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.
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:
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:
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:
Approach 3: Combination of For loop, split method and match method.
- First, we'll divide the string into an array of characters using the split method.
- Next, we'll create a variable named "n" and initialise it with the value 0.
- 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.
- 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:
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:
- How to split the string to array of characters.
- How to split the string to an array of words.
- Replace the multiple spaces present in the string with a single space.
- Check if string contains a special characters or characters other then alphanumeric values, space and dot.
- How to remove the numeric values present in the string using JavaScript.
- How to detect the numeric values present in the string using JavaScript.
- Extract the numeric values from a string.
- Count the words from a string.
- Count the characters from a string.
Share your thoughts.