4 ways in JavaScript to check if a string starts with a number

How to check if a string starts with a number in JavaScript:

In this post, we will discuss a common problem we encounter in JavaScript and other programming languages while working with strings. The problem is to check if a string starts with a number or not, i.e. to check if the first character of a string is a number or something else.

We will learn different ways to do that in this post.

Method 1: By using charCodeAt:

The charCodeAt method returns the unicode character at a given index or position of a string. This method takes the index of the character as the parameter and returns the unicode value of that character.

The index of the characters starts from 0 and ends at string length - 1. We can pass 0 as the parameter to this function to get the unicode value for the first character.

We need to compare the value with 48 and 57. The charCodeAt of ‘0’ is 48 and the charCodeAt of ‘9’ is 57.

So, we can write a function that returns true or false based on the unicode value of the first character of a string.

Below is the complete program:

const isFirstCharNum = (str) =>
  str.charCodeAt(0) > 47 && str.charCodeAt(0) < 58;

const strArrays = ["hello", "0123", "0hello", "#4676", "0", ""];

strArrays.forEach((str) =>
  console.log(`${str} starts with a number => ${isFirstCharNum(str)}\n`)
);

In this example,

  • isFirstCharNum function is used to check if the first character of a string is number or not. It returns one boolean value, true if the first character is a number, else it returns false.
  • The strArrays is an array of strings.
  • We are iterating over the strings of this array one by one and calling isFirstCharNum method to get the result.

If you run this program, it will print output as like below:

hello starts with a number => false

0123 starts with a number => true

0hello starts with a number => true

#4676 starts with a number => false

0 starts with a number => true

 starts with a number => false

JavaScript check string starts with number

Method 2: By using charAt:

We can also use the charAt method to get the same result. This method is used to get the character at a specific position of a string in JavaScript. It is defined as like below:

str.charAt(i)

It will return the character at the position i of the string str. We can compare this character with ‘0’ and ‘9’ to find if the character is a number character or not.

Let’s re-write the above program:

const isFirstCharNum = (str) => str.charAt(0) >= "0" && str.charAt(0) <= "9";

const strArrays = ["hello", "0123", "0hello", "#4676", "0", ""];

strArrays.forEach((str) =>
  console.log(`${str} starts with a number => ${isFirstCharNum(str)}\n`)
);

It will give the same output as the above program.

hello starts with a number => false

0123 starts with a number => true

0hello starts with a number => true

#4676 starts with a number => false

0 starts with a number => true

 starts with a number => false

Method 3: By using isNaN function:

The isNaN function is used to check if a number is not a number. NaN stands for Not-a-Number and isNaN() function returns true if a value is not a number. Else, it returns false.

Let’s take a look at the below program:

const testValues = [10, 10.5, -2.39, "0", "a", "23", "a233"];

testValues.forEach((str) => console.log(`${str} => ${!isNaN(str)}`));

It is printing the not value of isNaN for each of the values of testValues array. If you run this program, it will print the below output:

10 => true
10.5 => true
-2.39 => true
0 => true
a => false
23 => true
a233 => false

So, !isNaN() returns true if the parameter is a number, else it returns false.

Let’s use it to check if the first character of a string is number or not:

const isFirstCharNum = (str) => !isNaN(str.charAt(0));

const strArrays = ["hello", "0123", "0hello", "#4676", "0", ""];

strArrays.forEach((str) =>
  console.log(`${str} starts with a number => ${isFirstCharNum(str)}\n`)
);

If you run this program, it will print the below output:

hello starts with a number => false

0123 starts with a number => true

0hello starts with a number => true

#4676 starts with a number => false

0 starts with a number => true

 starts with a number => true

Exception:

For an empty string, it returns true. Because, empty string is interprets as 0 in JavaScript and isNaN returns false for it.

Method 4: By using a regular expression, Regex:

We can use regular expression to match the first character of a string and check if it is a number or not. To match the first character of a string and to check if it is a number or not, we need to use /^\d/. Here, / characters are used to match the start and end of the string, ^ defines the starting of the string and \d matched if the first character is a digit or not.

We can use this pattern in two ways. We can either use the match function of string or we can use the test function.

a) By using the match() function:

The match() function matches a string against a regular expression. It takes a regular expression object as the parameter and returns null if no matches are found or an array. We can check if the return is not null, then the string starts with a number.

Below is the complete program:

const isFirstCharNum = (str) => str.match(new RegExp(/^\d/)) !== null;

const strArrays = ["hello", "0123", "0hello", "#4676", "0", ""];

strArrays.forEach((str) =>
  console.log(`${str} starts with a number => ${isFirstCharNum(str)}\n`)
);

It will print:

hello starts with a number => false

0123 starts with a number => true

0hello starts with a number => true

#4676 starts with a number => false

0 starts with a number => true

 starts with a number => false

b) By using the test() function:

We can also use the test() function:

const isFirstCharNum = (str) => /^\d/.test(str);

const strArrays = ["hello", "0123", "0hello", "#4676", "0", ""];

strArrays.forEach((str) =>
  console.log(`${str} starts with a number => ${isFirstCharNum(str)}\n`)
);

It returns a boolean value. It will print the same result as the previous program.

You might also like: