In JavaScript, the string data type is used to store text data. The maximum possible string length depends on the browser and hardware, it can contain at least up to one hundred million characters. However, JS developers don't go to maximum length, as it can create significant difficulties in data processing.

String is often used to store phrases or text fragments. Therefore, JavaScript has built-in methods that offer ready-made solutions for finding and checking for the presence of certain fragments and make it easier to work with strings. The functionality of the startsWith() method allows you to find out if a string starts with a certain phrase (fragment).

Syntax

The startsWith() method has a very simple algorithm. It searches the target string in the text (or fragment) that starts with the search phrase and returns a response in Boolean. If the desired fragment was found, then it returns true, if not - false.

In the default state, startsWith() begins the search from the first character of the target string, so if the search phrase is in the middle of the string, the method will return false. But this can be avoided by using an extra parameter that startWith() takes.

This method can take 2 parameters, one of which is required:

  1. searchWord - required parameter to specify a string (of any length) that the method will search for in the target string. The search is performed on an exact match, so searchWord is case sensitive.

  2. searchFrom - optional numeric parameter that allows you to specify from which character of the target string the search will start.

When using only the searchWord parameter, the method syntax looks like this:

let words = 'method test ';
words.startsWith('method ');

Result. If we add output to the console, we will get the following execution result:

true

With the searchFrom parameter, you can specify the character (index) at which the search should be started. It's important to clarify that it looks for the part of the string which begins with the search phrase, not just contains it. The syntax and effect of the searchFrom parameter on the results of the startsWith() method use is:

let words = 'method test ';
words.startsWith('test ');
words.startsWith('test ', 6);
words.startsWith('test ', 7);

Result. If print results to the console, we get:

false
false
true

Test yourself

Task 1. Using startsWith() check if the given strings start with the word "The". Print the results of each check to the console.

let word1 = 'The United Nations';
let word2 = ' to the Moon';
let word3 = 'Then you need'
let word4 = ' Theatre';
let word5 = 'The The the';

Solution:

let word1 = 'The United Nations';
let word2 = 'to the Moon';
let word3 = 'Then you need'
let word4 = 'the bridge';
let word5 = 'The123the';
console.log(word1.startsWith('The')); // Output: true
console.log(word2.startsWith('The')); // Output: false
console.log(word3.startsWith('The')); // Output: true
console.log(word4.startsWith('The')); // Output: false
console.log(word5.startsWith('The')); // Output: true

Out of 5 lines, only 2nd and 4th words returned false. In word2 this happened because the string didn't start with the searchWord. Word4 was rejected - although it started with valid characters, they were not all case-matched.

Task 2. Using startsWith() and its parameters, prove that the test phrases contain the word "force" (or "Force").

let phrase1 = 'May the Force be with you. - Obi-Wan Kenobi';
let phrase2 = 'Water is the driving force of all nature. – Leonardo da Vinci';
let phrase3 = 'Peace cannot be kept by force; it can only be achieved by understanding. – Albert Einstein';

Solution. To solve this problem, it's enough to call startsWith() for each line using the searchFrom parameter. As values for it, we must use the indices of the search words in each of the phrases. Considering that the countdown starts from 0, it will be 8 for phrase1, 21 for phrase2, 24 for phrase3.

let phrase1 = 'May the Force be with you. - Obi-Wan Kenobi';
let phrase2 = 'Water is the driving force of all nature. – L. da Vinci';
let phrase3 = 'Peace cannot be kept by force; it can only be achieved by understanding. – A. Einstein';
console.log(phrase1.startsWith('Force', 8));
console.log(phrase2.startsWith('force', 21));
console.log(phrase3.startsWith('force', 24));

FAQ

What's the difference between startsWith and includes()?

The includes() method looks for occurrences of the search phrase anywhere in the string, while startsWith() only at the beginning.

What's the difference between startsWith and endsWith()?

The endsWith() method looks for a match only at the end of the string.

What's the difference between startsWith and indexOf()?

The indexOf() method returns the index (numeric value) of the string, while startsWith() returns the execution result in a Boolean.

What's the difference between startsWith and lastIndexOf()?

The lastIndexOf() method returns the index of the search string as a number, reading the array from the end to the beginning.