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

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

πŸ“– Usage Example
const fruits = ['apple', 'banana', 'cherry']
fruits.includes('banana')  // β†’ true
fruits.includes('grape')   // β†’ false

Use includes() to check whether a specific value exists in an array.

Array.prototype.includes()

Array.prototype.includes(value, fromIndex?) returns true if the array contains value, using the SameValueZero algorithm (like === but also handles NaN).

Challenge: Implement hasValue(arr, val) that returns arr.includes(val).

hasValue([1, 2, 3], 2) // β†’ true
hasValue([1, 2, 3], 5) // β†’ false
πŸ’‘ Hints (2)
  • β€’ `includes()` returns a boolean β€” true or false.
  • β€’ It uses SameValueZero comparison, similar to `===`, but also handles `NaN`.