JavaScript Program to Check if a String Starts With Another String

To understand this example, you should have the knowledge of the following JavaScript programming topics:


Example 1: Using startsWith()

Output

The string starts with "he".

In the above program, the startsWith() method is used to determine if the string starts with 'he'. The startsWith() method checks if the string starts with the particular string.

The if...else statement is used to check the condition.


Example 2: Using lastIndexOf()

Output

The string starts with "he".

In the above program, the lastIndexOf() method is used to check if a string starts with another string.

The lastIndexOf() method returns the index of the searched string (here searching from the first index).


Example 3: Using RegEx

Output

The string starts with "he".

In the above program, the string is checked using the RegEx pattern and the test() method.

/^ indicates the starting of the string.