JavaScript endsWith function not working

I have a web application. In one of the pages, I go all over the HTML element IDs wether one of them ends with a specified string or not. Every JS functions work on the page but "endsWith" function doesn't work. I really didn't understand the matter. Can anyone help?

var str = "To be, or not to be, that is the question."; alert(str.endsWith("question.")); 

The above simple JS code doesn't work at all?

10

4 Answers

As said in this post

var str = "To be, or not to be, that is the question."; function strEndsWith(str, suffix) { return str.match(suffix+"$")==suffix; } alert(strEndsWith(str,"question.")); 

this will return true if it ends with provided suffix.

JSFIDDLE

EDIT

There is a similar question asked before check it here

the answer says

var str = "To be, or not to be, that is the question$"; String.prototype.endsWith = function(suffix) { return this.indexOf(suffix, this.length - suffix.length) !== -1; }; alert(str.endsWith("$")); 
2

ES5 has no endsWith function (or, for that matter, startsWith). You can roll your own, like this version from MDN:

if (!String.prototype.endsWith) { Object.defineProperty(String.prototype, 'endsWith', { enumerable: false, configurable: false, writable: false, value: function (searchString, position) { position = position || this.length; position = position - searchString.length; var lastIndex = this.lastIndexOf(searchString); return lastIndex !== -1 && lastIndex === position; } }); } 
7

I have never seen an endsWith function in JS. You can rather do an String.length and then check the last words by manually referencing each character you want to check against.

Even better would be to do a regex to find the last word in the string and then use that (Regular expression to find last word in sentence).

I found the endsWith() function available in Chrome console, but oddly, not defined when debugging in VS Code (with Chrome). You can try editing the snippet below by deleting the polyfill to see if your browser supports it.

This is a quote from MDN Developer Docs for String.prototype.endsWith():

String.prototype.endsWith()

This method has been added to the ECMAScript 6 specification and may not be available in all JavaScript implementations yet. However, you can polyfill String.prototype.endsWith() with the following snippet:

// If string.endsWith() isn't defined, Polyfill it. if (!String.prototype.endsWith) { String.prototype.endsWith = function(search, this_len) { if (this_len === undefined || this_len > this.length) { this_len = this.length; } return this.substring(this_len - search.length, this_len) === search; }; } // Use it. const myString = "Mayberry"; const result = myString.endsWith("berry") ? 'Yes' : 'Nope'; document.body.append('A. ' + result);
Q. Does Mayberry end with "berry"?<br>

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like