--- title: String.prototype.endsWith() slug: Web/JavaScript/Reference/Global_Objects/String/endsWith tags: - JavaScript - Prototipo - Referencia - String - metodo translation_of: Web/JavaScript/Reference/Global_Objects/String/endsWith original_slug: Web/JavaScript/Referencia/Objetos_globales/String/endsWith ---
El método endsWith() determina si una cadena de texto termina con los caracteres de una cadena indicada, devolviendo true o false según corresponda.
str.endsWith(searchString[, position])
searchStringstr.length {{optional_inline}}str. Por defecto se usa str.length.true si los caracteres proporcionados se encuentran al final de la cadena de texto; en caso contrario, false.
Este método determina si una cadena de texto termina en otra cadena o no. Este método distingue entre mayúsculas y minúsculas.
Este método ha sido añadido a la especificación ECMAScript 6 y puede no estar disponible en todas las implementaciones de JavaScript. Sin embargo, puedes implementar el polyfill String.prototype.endsWith() con el siguiente fragmento de código:
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;
};
}
endsWith()let 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
| Specification |
|---|
| {{SpecName('ESDraft', '#sec-string.prototype.endswith', 'String.prototype.endsWith')}} |
{{Compat("javascript.builtins.String.endsWith")}}