String.prototype.includes()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.

El método includes() determina si una cadena de texto puede ser encontrada dentro de otra cadena de texto, devolviendo true o false según corresponda.

Pruébalo

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 posición dentro de la cadena en la cual empieza la búsqueda de searchString (Por defecto este valor es 0).

Valor devuelto

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

Descripción

Este método permite determinar si una cadena de texto se encuentra incluida dentro de la otra.

Sensibilidad a Mayúsculas/Minúsculas

El método includes() es "case sensitive" (tiene en cuenta mayúsculas y minúsculas). Por ejemplo, la siguiente expresión devolverá false:

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

Polyfill

Este método ha sido agregado a la especificación ECMAScript 2015 y puede no estar disponible en toda las implementaciones de JavaScript.

Sin embargo, puedes usar este método 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® 2025 Language Specification
# sec-string.prototype.includes

Compatibilidad con navegadores

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
includes

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Uses a non-standard name.
Has more compatibility info.

Ver también