Categories
JavaScript Answers

How to check if a string ends with a substring with JavaScript?

Spread the love

Sometimes, we want to check if a string ends with a substring with JavaScript.

In this article, we’ll look at how to check if a string ends with a substring with JavaScript.

How to check if a string ends with a substring with JavaScript?

To check if a string ends with a substring with JavaScript, we can use the string endsWith method.

For instance, we write

const endsWithDog = "this is dog".endsWith("dog");

to call "this is dog".endsWith with 'dog' to check if "this is dog" ends with 'dog'.

It should return true since 'dog' is at the end of "this is dog".

Conclusion

To check if a string ends with a substring with JavaScript, we can use the string endsWith method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *