indexOf and includes both answer "is this value in the array?", but not always the same way. [NaN].indexOf(NaN) is -1 while [NaN].includes(NaN) is true, and only one of them treats a hole as undefined. Rewriting both shows exactly where they differ, and why includes was added to the language.
Write Array.prototype.myIndexOf(searchElement, fromIndex) and Array.prototype.myIncludes(searchElement, fromIndex) in search.js. They must behave like the built-in indexOf and includes for every case below.
Array.prototype.myIndexOf=functionmyIndexOf(searchElement, fromIndex) // returns an index or -1Array.prototype.myIncludes = functionmyIncludes(searchElement, fromIndex) // returns true or false
searchElement: the value to look for.
fromIndex: where the search starts (0 when missing). A negative value counts back from the end; it is converted to a whole number first.
myIndexOf returns the first index whose item is === to the value, or -1. No conversion: "1" does not match 1; objects match only themselves.
myIndexOf never finds NaN, treats 0 and -0 as equal, and skips holes.
myIncludes returns true or false. It compares like ===, except that NaN equals NaN. It does not skip holes: a hole reads as undefined.
Both start at fromIndex. A negative fromIndex starts at length + fromIndex, and at 0 if that is still negative. A start at or past the end finds nothing.
Both convert fromIndex: "1" and 1.9 are 1, NaN and -0.5 are 0. They convert it once, and not at all when the array is empty. A fromIndex that is a BigInt or a Symbol, or an object whose valueOf returns one, throws a TypeError (when the array is not empty), as the built-ins do.
Both work on any object with a length (an array-like object, or a string), called with Array.prototype.myIndexOf.call(…), and read length once. length is converted to a whole number from 0 up: "2" is 2, 2.9 is 2, -1 is 0; a BigInt or Symbol length throws a TypeError.
Both throw a TypeError when called on null or undefined.
Plain JavaScript in a browser page; the tests call Array.prototype.myIndexOf and Array.prototype.myIncludes from search.js.
search.js starts with "use strict". Keep it: in strict mode, this inside the methods is exactly the value they were called on, so a call on null can be detected.
Not allowed: the built-in indexOf, lastIndexOf, includes, find, findIndex, findLast, findLastIndex and some. The tests delete them from Array.prototype while your methods run. Number.isNaN and Object.is are allowed.