JS Built-ins
← Array109 / 226
ArrayArray.prototype.indexOfbeginner

Array.prototype.indexOf() β€” find word index

πŸ“– Usage Example
const words = ['cat', 'dog', 'bird']
words.indexOf('dog')   // β†’ 1
words.indexOf('fish')  // β†’ -1

Use indexOf() to find the index of a specific string in an array.

Array.prototype.indexOf() β€” strings

indexOf works with string arrays too, using case-sensitive strict equality.

Challenge: Implement findWord(words, word) that returns the index of word in the array.

findWord(['foo', 'bar', 'baz'], 'bar') // β†’ 1
findWord(['foo', 'bar'], 'qux')        // β†’ -1
πŸ’‘ Hints (2)
  • β€’ `indexOf` is case-sensitive β€” `"hello" !== "Hello"`.
  • β€’ For case-insensitive search, you would need `findIndex` with a `.toLowerCase()` comparison.