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.