JS: Array.prototype.indexOf
myArray.indexOf(value)
-
- Return the index of first element that is value.
- Return -1 if not found.
🛑 WARNING: It cannot find NaN. To check NaN, use JS: Array.prototype.includes.
console.log([3, 4, 5, 6].indexOf(5) === 2); console.log(["b", 5, 6, "b"].indexOf("b", 1) === 3);
// cannot find NaN console.log([NaN, NaN].indexOf(NaN) === -1);
// finds undefined that exist as array element console.log([3, undefined].indexOf(undefined) === 1); // HHHH------------------------------ // sparse array const xx = [3, 4]; xx.length = 100; // cannot find non-existent element console.log(xx.indexOf(undefined) === -1); // true
myArray.indexOf(value, startIndex)
-
Search starts at startIndex.
console.log([3, 3].indexOf(3, -1) === 1);