This rule enforces the use of Array.includes() over manual indexOf() !== -1 checks for better readability and performance.
- if (arr.indexOf(item) !== -1) {
+ if (arr.includes(item)) {
console.log('Item found')
}- const found = arr.indexOf(item) !== -1
+ const found = arr.includes(item) function hasItem(arr, item) {
- return arr.indexOf(item) !== -1
+ return arr.includes(item)
}- const checkExists = (items, target) => items.indexOf(target) !== -1
+ const checkExists = (items, target) => items.includes(target)- if (getArray().indexOf(getItem()) !== -1) {
+ if (getArray().includes(getItem())) {
console.log('Found')
}- const exists = items.indexOf(item) !== -1
+ const exists = items.includes(item)- if (obj.items.indexOf(item) !== -1) {
+ if (obj.items.includes(item)) {
console.log('Item in collection')
}- const inList = user.favorites.indexOf(book) !== -1
+ const inList = user.favorites.includes(book)- const result = arr.indexOf(item) !== -1 ? 'found' : 'not found'
+ const result = arr.includes(item) ? 'found' : 'not found'- const status = items.indexOf(target) !== -1 && 'active'
+ const status = items.includes(target) && 'active'This rule only applies to:
- ✅
array.indexOf(item) !== -1comparisons - ✅ Variable assignments with
indexOf() !== -1 - ✅ Return statements with
indexOf() !== -1 - ✅ Conditional expressions with
indexOf() !== -1
This rule does NOT apply to:
- ❌
array.indexOf(item) === -1(not found checks) - ❌
array.indexOf(item) > -1orarray.indexOf(item) >= 0 - ❌ Already using
Array.includes() - ❌ Other array methods (
find,some,every, etc.) - ❌
indexOf()with assignment or other operators
This rule applies to the following operations:
- ✅
arr.indexOf(item) !== -1→arr.includes(item) - ✅
items.indexOf(target) !== -1→items.includes(target) - ✅
getArray().indexOf(getItem()) !== -1→getArray().includes(getItem())
The rule provides auto-fix suggestions that convert indexOf() !== -1 patterns to Array.includes():
arr.indexOf(item) !== -1→arr.includes(item)items.indexOf(target) !== -1→items.includes(target)getArray().indexOf(getItem()) !== -1→getArray().includes(getItem())