Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Array.prototype.includes

Checks whether an array contains a value.

Available since: ES2016

Syntax

array.includes(searchElement, fromIndex)

Parameters

ParameterTypeDescription
searchElementanyThe value to look for.
fromIndexnumberOptional. The index to start searching from.

Return value

boolean`true` if the value is found, `false` otherwise.

Examples

Looking for a value:

console.log(['a', 'b', 'c'].includes('b'));
console.log(['a', 'b', 'c'].includes('z'));

Output:

true
false

The comparison is strict: the string '1' does not equal the number 1:

console.log([1, 2, 3].includes('1'));

Output:

false

Notes

Objects are compared by reference, so the method cannot find an object with the same properties — use some() or find() for that.

See also

Related recipes

In the tutorial

  • Searching arrays — How to find an element in a JavaScript array: includes, indexOf, find, findIndex, some and every.

Thanks for reading!

Donate to the site — we work hard for you.