[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/includes [Back]  [Original]

Array.prototype.includes() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Array.prototype.includes()

20168

includes() true false

const array1 = [1, 2, 3];

console.log(array1.includes(2));
// Expected output: true

const pets = ["cat", "dog", "bat"];

console.log(pets.includes("cat"));
// Expected output: true

console.log(pets.includes("at"));
// Expected output: false

js
includes(searchElement)
includes(searchElement, fromIndex)

searchElement

fromIndex

  • fromIndex < 0 fromIndex + array.length
  • fromIndex < -array.length fromIndex 0
  • fromIndex >= array.length false

fromIndex fromIndex searchElement true

includes() searchElement 0 -0 0 false 0 NaN

includes() undefined

includes() this length

includes()

js
[1, 2, 3].includes(2); // true
[1, 2, 3].includes(4); // false
[1, 2, 3].includes(3, 3); // false
[1, 2, 3].includes(3, -1); // true
[1, 2, NaN].includes(NaN); // true
["1", "2", "3"].includes(3); // false

fromIndex

fromIndex false

js
const arr = ["a", "b", "c"];

arr.includes("c", 3); // false
arr.includes("c", 100); // false

0

fromIndex searchElement 0

js
//  3
// fromIndex  -100
//  3 + (-100) = -97

const arr = ["a", "b", "c"];

arr.includes("a", -100); // true
arr.includes("b", -100); // true
arr.includes("c", -100); // true
arr.includes("a", -2); // false

includes()

undefined true

js
console.log([1, , 3].includes(undefined)); // true

includes()

includes() this length

js
const arrayLike = {
  length: 3,
  0: 2,
  1: 3,
  2: 4,
};
console.log(Array.prototype.includes.call(arrayLike, 2));
// true
console.log(Array.prototype.includes.call(arrayLike, 1));
// false

ECMAScript 2027 LanguageSpecification
# sec-array.prototype.includes


Web Proxy Viewer  |  New URL  |  Original Page