Check if a string begins with a number in JavaScript
This post will discuss how to check if a string begins with a number in JavaScript.
There are several ways to determine if a string begins with a number in JavaScript. Here are some of the most common functions:
1. Using regular expressions
A built-in function of the string object called match() can help we with this. It accepts a regular expression as an argument and gives us an array of all the matches in the string, or null if there are no matches. We can use the regular expression /^\d/ to match with any string that begins with a digit. For example, we can determine if a string begins with a number using match() like this:
|
1 2 3 4 5 6 7 |
function startsWithNumber(str) { return str.match(/^\d/) != null; } let str = "221B Baker Street"; let result = startsWithNumber(str); console.log(result); // true |
Alternatively, we can use the test() function of a regular expression object to check if a string matches the pattern. We can use it to check if a string begins with a number as follows:
|
1 2 3 4 5 6 7 |
function startsWithNumber(str) { return /^\d/.test(str); } let str = "221B Baker Street"; let result = startsWithNumber(str); console.log(result); // true |
2. Using charCodeAt() function
Another way is to use the charCodeAt() function, which is also a built-in function of the string object. This function takes an index as an argument and returns the Unicode value of the character at that index in the string. We can use this function to get the Unicode value of the first character of a string, and compare it with the range of values for numbers (48 to 57). We can use it to check if the first character of a string is a digit as follows:
|
1 2 3 4 5 6 7 8 |
function startsWithNumber(str) { let charCode = str.charCodeAt(0); return charCode >= 48 && charCode <= 57; } let str = "221B Baker Street"; let result = startsWithNumber(str); console.log(result); // true |
Alternatively, we can get the first character of the string and compare it with the range of digits from '0' to '9'. This will determine whether a string has a numeric prefix. For example:
|
1 2 3 4 5 6 7 |
function startsWithNumber(str) { return str[0] >= '0' && str[0] <= '9'; } let str = "221B Baker Street"; let result = startsWithNumber(str); console.log(result); // true |
3. Using isNaN() function
A third way is to use the isNaN() function, which is a global function that checks if a value is not a number. This function returns true if the value is not a number, and false otherwise. We can use this function to check if the first character of a string is not a number, and negate the result to get a boolean value. For example, using the same string as before, we can determine if it begins with a number using isNaN() like this:
|
1 2 3 4 5 6 7 |
function startsWithNumber(str) { return !isNaN(str[0]); } let str = "221B Baker Street"; let result = startsWithNumber(str); console.log(result); // true |
4. Using startsWith() function
Finally, to check if a string begins with any number, we can use a loop to iterate over the range of digits from '0' to '9', and call the startsWith() function with each digit as an argument. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
function startsWithNumber(str) { for (let i = '0'; i <= '9'; i++) { if (str.startsWith(i)) { return true; } } return false; } let str = "221B Baker Street"; let result = startsWithNumber(str); console.log(result); // true |
That’s all about checking if a string begins with a number in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)