JavaScript String endsWith() method

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

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

const string1 = 'Hello, world!';

const result = string1.endsWith('world!');

// 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 endsWith() method to check if the string1 string ends with the suffix world!. The endsWith() method takes a single argument, which is the suffix to search for at the end of the string. In this case, we pass in the string world! as the argument to the endsWith() method.

As a result of calling the endsWith() method, the result variable will be set to true, because the string1 string does indeed end with the suffix world!. If the string1 string did not end with the suffix world!, 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 )

Facebook photo

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

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: