JavaScript String endsWith

The endsWith() method determines whether a string ends with the specific character or not, It returns true if the string ends with specific character or string, otherwise returns false.


yourString.endsWith("searchString", [position]);


searchString: A characters or strings to be searched at the end of the string.

position: It is a optional parameters, It indicates that the search within this limit, even if the string is too long, If we can not pass this arguments than it automatically becomes the the length of the string.


This method allow you to determine whether the string is ends with the specific character (sub-string) or not.

The endsWith() method is case-sensitive, it will not ignore the case of the characters.

var yourString = "JavaScript Hive";
document.write(yourString.endsWith("Hive")+""); //true

Note: This method will not worked on the old browsers, It will works only the latest browsers because this method is added on the ECMAScript 6. As per the ECMAScript 6 specification it may not be available in all JavaScript Engine.


If client using the old browser and that browser does not support the endsWith() method than you code will not work on that browser so your operation will be terminated, If you check the developer window in old browsers than you get the message like this:

Object doesn't support property or method 'endsWith'

But I created one code for workaround it and it will worked on the all the browsers, I hope it will help you.

if (typeof String.prototype.endsWith !== 'function') {
    String.prototype.endsWith = function (searchString, position) {
        if (position == null || position < 0 || Math.floor(position) !== position) {
            position = this.length;
        position -= searchString.length;
        return this.indexOf(searchString, position) !== -1;

var yourString = "JavaScript Hive";
document.write(yourString.endsWith("Hive") + ""); //true
document.write(yourString.endsWith("Script") + ""); //false
document.write(yourString.endsWith("Script", 10)); //true

The magic of the above code is that if your user accessing the your website with newer browser that support the endsWith() method than the native method is called instead of the our method and if user is using the older browser than the our method is executing, Its magic because the calling of the both method is same.

Explicitly we just add the endsWith() method in the String class if there is not already exist.

Above function will works on the all the older browsers, even with the Internet Explore (IE) browser.

Case-Insensitive Search:

If you want to check that the string have the specific sub-string by ignoring its case, than below code will help you to find out it.

var yourString = "JavaScript Hive";
var searchString = "HIVE";

if (yourString.toLowerCase().indexOf(searchString.toLowerCase()) != -1) {

The above code also helps to check you that the string is ends with the specific sub-string or not by ignoring it’s case.

Happy Coding!!!