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

String.prototype.includes() - 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.includes()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since septiembre de 2015.

El mtodo includes() determina si una cadena de texto puede ser encontrada dentro de otra cadena de texto, devolviendo true o false segn corresponda.

In this article

Prubalo

const sentence = "The quick brown fox jumps over the lazy dog.";

const word = "fox";

console.log(
  `The word "${word}" ${
    sentence.includes(word) ? "is" : "is not"
  } in the sentence`,
);
// Expected output: "The word "fox" is in the sentence"

Sintaxis

str.includes(searchString[, position])

Parametros

searchString

Una cadena a buscar en el texto str.

position Opcional

La posicin dentro de la cadena en la cual empieza la bsqueda de searchString (Por defecto este valor es 0).

Valor devuelto

true si la cadena de texto contiene la cadena buscada; en caso contrario, false.

Descripcin

Este mtodo permite determinar si una cadena de texto se encuentra incluida dentro de la otra.

Sensibilidad a Maysculas/Minsculas

El mtodo includes() es "case sensitive" (tiene en cuenta maysculas y minsculas). Por ejemplo, la siguiente expresin devolver false:

js
"Ballena azul".includes("ballena"); // devuelve false

Polyfill

Este mtodo ha sido agregado a la especificacin ECMAScript 2015 y puede no estar disponible en toda las implementaciones de JavaScript.

Sin embargo, puedes usar este mtodo como polyfill:

js
if (!String.prototype.includes) {
  String.prototype.includes = function (search, start) {
    "use strict";

    if (search instanceof RegExp) {
      throw TypeError("first argument must not be a RegExp");
    }
    if (start === undefined) {
      start = 0;
    }
    return this.indexOf(search, start) !== -1;
  };
}

Ejemplos

Usando includes()

js
const str = "To be, or not to be, that is the question.";

console.log(str.includes("To be")); // true
console.log(str.includes("question")); // true
console.log(str.includes("nonexistent")); // false
console.log(str.includes("To be", 1)); // false
console.log(str.includes("TO BE")); // false
console.log(str.includes("")); // true

Especificaciones

Specification
ECMAScript 2027 LanguageSpecification
# sec-string.prototype.includes

Compatibilidad con navegadores

Ver tambin


Web Proxy Viewer  |  New URL  |  Original Page