The includes() method is used to check whether an array contains a specific element.
- Checks if an element exists in an array
- Returns true or false
- Uses strict equality (===)
- Does not modify the original array
1.Syntax
- searchElement → value to find
- fromIndex (optional) → index to start searching from
2.Basic Example
3.Using fromIndex
4.includes() with NaN
Unlike indexOf(), includes() can detect NaN.
5.includes() vs indexOf()
| Feature | includes() |
indexOf() |
|---|---|---|
| Return type | Boolean | Index / -1 |
Detects NaN |
Yes | No |
| Readability | Better | Less clear |
6.Using includes() with Objects
⚠ Objects are compared by reference, not value.
