The endsWith()
method determines whether a string ends with the characters of another string, returning true
or false
as appropriate.
str.endsWith(searchString[, position])
searchString
position
This method lets you determine whether or not a string ends with another string.
endsWith()
var str = 'To be, or not to be, that is the question.'; console.log(str.endsWith('question.')); // true console.log(str.endsWith('to be')); // false console.log(str.endsWith('to be', 19)); // true
This method has been added to the ECMAScript 6 specification and may not be available in all JavaScript implementations yet. However, you can polyfill str.endsWith()
with the following snippet:
if (!str.endsWith) { str.endsWith = function(searchString, position) { var subjectString = this.toString(); if (position === undefined || position > subjectString.length) { position = subjectString.length; } position -= searchString.length; var lastIndex = subjectString.indexOf(searchString, position); return lastIndex !== -1 && lastIndex === position; }; }
Created by Mozilla Contributors, license: CC-BY-SA 2.5