JavaScript String startsWith() method

The startsWith() method in JavaScript is used to check whether a string begins with the specified substring. This method takes a single argument, which is the substring to search for at the beginning of the string. If the substring is found at the beginning of the string, the startsWith() method returns true. If the substring is not found at the beginning of the string, the startsWith() method returns false.

Here is an example of how the startsWith() method works:

const string1 = 'Hello, world!';

const result = string1.startsWith('Hello');

// the result will be: true

In the code example above, we first declare a string called string1 that contains the text Hello, world!. We then use the startsWith() method to check whether the string1 string begins with the substring Hello. The startsWith() method takes a single argument, which is the substring to search for at the beginning of the string. In this case, we pass in the string Hello as the argument to the startsWith() method.

As a result of calling the startsWith() method, the result variable will be set to true, indicating that the string1 string begins with the substring Hello. If the string1 string did not begin with the substring Hello, the result variable would be set to false.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: