JavaScript: String.prototype.endsWith

By Xah Lee. Date: . Last updated: .

New in JS2015.

str.endsWith( searchString )
Return true if str end with searchString. Else, return false.
str.endsWith( searchString, endPosition )
Use index endPosition as the end of string.

endPosition is negative, count from right.

const ss = "abcd";

console.log( ss.endsWith("cd") ); // true
console.log( ss.endsWith("cd", 4) ); // true
console.log( ss.endsWith("a", 1) ); // true
console.log( ss.endsWith("ab", 2) ); // true

console.log( ss.endsWith("b") );  // false
console.log( ss.endsWith("bc", 2) ); // false
JS in Depth
XAH  BUY NOW

JS in Depth

JS Obj Ref

DOM


JS Obj Ref

String

prototype