[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith [Back]  [Original]

String.prototype.endsWith() - JavaScript | MDN

Esta pgina ha sido traducida del ingls por la comunidad. Aprende ms y nete a la comunidad de MDN Web Docs.

View in English Always switch to English

String.prototype.endsWith()

Baseline
Ampliamente disponible

Esta caracterstica est consolidada y funciona en muchos dispositivos y versiones de navegador. Est disponible en todos los navegadores desde septiembre de 2015.

El mtodo endsWith() determina si una cadena de texto termina con los caracteres de una cadena indicada, devolviendo true o false segn corresponda.

En este artculo

Prubalo

const str1 = "Cats are the best!";

console.log(str1.endsWith("best!"));
// Expected output: true

console.log(str1.endsWith("best", 17));
// Expected output: true

const str2 = "Is this a question?";

console.log(str2.endsWith("question"));
// Expected output: false

Sintaxis

str.endsWith(searchString[, position])

Parmetros

searchString

Los caracteres a buscar hasta el final de la cadena str.

length Opcional

Si se indica, se utiliza como el tamao de str. Por defecto se usa str.length.

Valor devuelto

true si los caracteres proporcionados se encuentran al final de la cadena de texto; en caso contrario, false.

Descripcin

Este mtodo determina si una cadena de texto termina en otra cadena o no. Este mtodo distingue entre maysculas y minsculas.

Polyfill

Este mtodo ha sido aadido a la especificacin 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 cdigo:

js
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;
  };
}

Ejemplos

Usando endsWith()

js
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

Especificaciones

Especificacin
ECMAScript 2027 LanguageSpecification
# sec-string.prototype.endswith

Compatibilidad con navegadores

Ver tambin


Web Proxy Viewer  |  New URL  |  Original Page