Hello guys. Welcome back. Today we will learn how to check if string starts with Number using JavaScript. This is a problem that all new and experienced programmers are still searching for a solution. To do it, we tested effectively with regular expressions with the match()
method, the regex with the test()
method, and the ASCII Code. Please read to the end of the articles to know how we can do it.
To Check If String Starts With Number Using JavaScript
Using regular expression with the match()
method
Firstly, we use the match()
method to call a string. And use the regular expression ^\d
is passed as an argument to the match()
method.
- ^ to check only the first character on the string
- \d to verify the character is Number.
- The / and / must cover the start and end of the expression
- If the first character is the Number, this code will print out the console ‘The string ‘str’ starts with a number. Otherwise, print does not start with Number.
Code Example
Let’s see the code example below.
function checkStr(str){ // Using the match() method and ^ to check the first character in string is \d(number) or not if (str.match(/^\d/)) { console.log("The string:", str, "starts with the number"); }else{ console.log("The string:", str, "does not start with a number"); } }; checkStr('2023 - Happy New Year'); checkStr('LearnShareIT - Happy New Year 2023'); checkStr('1JavaScript');
Output
The string: 2023 - Happy New Year starts with Number
The string: LearnShareIT - Happy New Year 2023 does not start with Number
The string: 1JavaScript starts with Number
Using the regex and test()
method
The second way to check if the string starts with a number using JavaScript is using the test()
method to find out the match between /^\d/ and str. The result will show the string starting with a number if the match is found. Otherwise, the string does not start with Number.
Code Example
Let’s see the code example below.
function checkStr(str){ // Using the test() method to find out the match between /^\d/ and str if (/^\d/.test(str)) { console.log("The string:", str, "starts with the Number"); }else{ console.log("The string:", str, "does not start with the Number"); } }; checkStr('2023 - Happy New Year'); checkStr('LearnShareIT - Happy New Year 2023'); checkStr('1JavaScript');
Output
Using the ASCII code and charCodeAt()
method.
The last way we want to introduce this article is by using the ASCII code.
In the example below, we check whether the first character falls between 48 and 57 by using the charCodeAt()
method. The result will show the string starts with the Number if it is true. Otherwise, start with something other than Number.
Code Example
Let’s see the code example below.
function checkStr(str){ // Using charCodeAt(0) method to check the first character in the string if (str.charCodeAt(0) >= 48 && str.charCodeAt(0) <= 57) { console.log("The string:", str, "starts with the Number"); }else{ console.log("The string:", str, "does not start with the Number"); } }; checkStr('2023 - Happy New Year'); checkStr('LearnShareIT - Happy New Year 2023'); checkStr('1JavaScript');
Output
The string: 2023 - Happy New Year starts with the Number
The string: LearnShareIT - Happy New Year 2023 does not start with the Number
The string: 1JavaScript starts with the Number
Summary
Hopefully, through this article, you can quickly “check if the string starts with Number” in JavaScript using the three methods mentioned above. But we prefer using the regex and test()
method, as it is straightforward and quick to solve this problem. How about you? Leave your comment here if you have any questions about this article.
Thank you for reading!

My name is Fred Hall. My hobby is studying programming languages, which I would like to share with you. Please do not hesitate to contact me if you are having problems learning the computer languages Java, JavaScript, C, C#, Perl, or Python. I will respond to all of your inquiries.
Name of the university: HUSC
Major: IT
Programming Languages: Java, JavaScript, C , C#, Perl, Python