JS Built-ins
← Array103 / 226
ArrayArray.prototype.includesbeginner

Array.prototype.includes() β€” check for a word

πŸ“– Usage Example
const colors = ['red', 'green', 'blue']
colors.includes('green')  // β†’ true
colors.includes('yellow') // β†’ false

Use includes() for a readable boolean check instead of indexOf() !== -1.

Array.prototype.includes() β€” strings

includes() works with any element type. It is the cleanest way to check if a string exists in an array of strings.

Challenge: Implement hasWord(words, word) that returns true if word is in the words array.

hasWord(['hello', 'world'], 'world') // β†’ true
hasWord(['hello', 'world'], 'foo')   // β†’ false
πŸ’‘ Hints (2)
  • β€’ `includes()` is case-sensitive β€” `"Hello" !== "hello"`.
  • β€’ This is simpler and more readable than `words.indexOf(word) !== -1`.