Skip to curriculum
Jump to the editor

3.6. indexOf and includes

Medium
Topics
15–30 min

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.

Signature

JAVASCRIPT
Array.prototype.myIndexOf = function myIndexOf(searchElement, fromIndex)   // returns an index or -1
Array.prototype.myIncludes = function myIncludes(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.

Requirements

  1. myIndexOf returns the first index whose item is === to the value, or -1. No conversion: "1" does not match 1; objects match only themselves.
  2. myIndexOf never finds NaN, treats 0 and -0 as equal, and skips holes.
  3. myIncludes returns true or false. It compares like ===, except that NaN equals NaN. It does not skip holes: a hole reads as undefined.
  4. 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.
  5. 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.
  6. 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.
  7. Both throw a TypeError when called on null or undefined.

Examples

JAVASCRIPT
["a", "b", "a"].myIndexOf("a");       // 0
["a", "b", "a"].myIndexOf("a", 1);    // 2
[1, 2, 1, 2].myIndexOf(2, -1);        // 3: starts at the last item
[1, 2].myIndexOf("1");                // -1: no conversion
JAVASCRIPT
[NaN].myIndexOf(NaN);                 // -1
[NaN].myIncludes(NaN);                // true
JAVASCRIPT
[1, , 3].myIndexOf(undefined);        // -1: the hole is skipped
[1, , 3].myIncludes(undefined);       // true: the hole reads as undefined

Environment

  • 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.
  • lastIndexOf is out of scope.

Hints

  1. Which value is the only one in JavaScript that is not equal to itself?
  2. The two methods compute their starting index the same way. What should happen when length + fromIndex is still below 0?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…