Array.prototype.indexOf returns the position of the first element that strictly equals a value, or -1 if there isn't one. It's the older sibling of includes: where includes gives you a yes/no, indexOf gives you where — and it compares with plain ===, so it can't find NaN.
Implement arrayIndexOf(arr, value, fromIndex). Scan from fromIndex (default 0) to the end, returning the index of the first element === value, or -1. A negative fromIndex counts back from the end.
function arrayIndexOf(arr, value, fromIndex) {
// returns the first index i (>= fromIndex) where arr[i] === value,
// or -1 if none; negative fromIndex counts from the end.
}
arrayIndexOf(['a', 'b', 'c'], 'b'); // 1
arrayIndexOf([5, 6, 5], 5); // 0 — the FIRST match
arrayIndexOf([1, 2, 3], 9); // -1 — not found
arrayIndexOf([5, 6, 5], 5, 1); // 2 — start at index 1, so skip the first 5
arrayIndexOf([1, NaN], NaN); // -1 — === can't match NaN (use includes for that)
===. indexOf([0], '') is -1; no type coercion.fromIndex — defaults to 0; a negative value resolves to arr.length + fromIndex, clamped to 0.indexOf can never find NaN, because NaN === NaN is false. That's the one job includes does and indexOf doesn't.indexOf won't return its index.