| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/String/includes | [Back] [Original] |
Get to know MDN better
Esta pgina ha sido traducida del ingls por la comunidad. Aprende ms y nete a la comunidad de MDN Web Docs.
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.
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"
str.includes(searchString[, position])
searchStringUna cadena a buscar en el texto str.
position OpcionalLa posicin dentro de la cadena en la cual empieza la bsqueda de searchString (Por defecto este valor es 0).
true si la cadena de texto contiene la cadena buscada; en caso contrario, false.
Este mtodo permite determinar si una cadena de texto se encuentra incluida dentro de la otra.
El mtodo includes() es "case sensitive" (tiene en cuenta maysculas y minsculas). Por ejemplo, la siguiente expresin devolver false:
"Ballena azul".includes("ballena"); // devuelve false
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:
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;
};
}
includes()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
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-string.prototype.includes |
Array.prototype.includes()TypedArray.prototype.includes()String.prototype.indexOf()String.prototype.lastIndexOf()String.prototype.startsWith()String.prototype.endsWith()This page was last modified on 11 feb 2025 by MDN contributors.
StringString.prototype.anchor()at()String.prototype.big()String.prototype.blink()String.prototype.bold()String.prototype.charAt()String.prototype.charCodeAt()String.prototype.codePointAt()String.prototype.concat()String.prototype.endsWith()String.prototype.fixed()String.prototype.fontcolor()String.prototype.fontsize()String.prototype.includes()String.prototype.indexOf()isWellFormed()String.prototype.italics()String.prototype.lastIndexOf()String.prototype.link()String.prototype.localeCompare()String.prototype.match()String.prototype.matchAll()String.prototype.normalize()padEnd()String.prototype.padStart()String.prototype.repeat()String.prototype.replace()String.prototype.replaceAll()String.prototype.search()String.prototype.slice()String.prototype.small()String.prototype.split()String.prototype.startsWith()String.prototype.strike()String.prototype.sub()String.prototype.substr()String.prototype.substring()String.prototype.sup()String.prototype.toLocaleLowerCase()String.prototype.toLocaleUpperCase()String.prototype.toLowerCase()String.prototype.toString()String.prototype.toUpperCase()toWellFormed()String.prototype.trim()String.prototype.trimEnd()trimStart()String.prototype.valueOf()[Symbol.iterator]()Object/FunctionYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |