New in ES2016.
myArray.includes ( searchElement )
myArray.includes ( searchElement, fromIndex )
is in myArray. Else,
fromIndex defaults to 0.
fromIndex can be negative. If negative, index count from right to left starting at last element.
The includes method intentionally differs from the similar indexOf method in two ways. First, it uses the SameValueZero algorithm, instead of Strict Equality Comparison, allowing it to detect NaN array elements. Second, it does not skip missing array elements, instead treating them as undefined.
[see JS: NaN]
const rr = [3,4,5]; console.log ( rr.includes (4) === true ); // true
console.log ( [3,NaN,5].includes(NaN) === true ); // true
console.log ( [3,undefined,5].includes(undefined) === true ); // true
Sparse array example:
const arr = [3,4]; arr.length = 100; console.log ( arr.includes(undefined) === true ); // true console.log ( arr.indexOf ( undefined ) === -1 ); // true
[see JS: Sparse Array]
JS Array Search
If you have a question, put $5 at patreon and message me.