Enforces consistent style for element existence checks with indexOf(), lastIndexOf(), findIndex(), and findLastIndex().
Prefer using index === -1 to check if an element does not exist and index !== -1 to check if an element does exist.
These methods return -1 when an element is not found, so checking against -1 is more explicit and precise than using broad comparisons like < 0 or >= 0.
Array<string>.indexOf(searchElement: string, fromIndex?: number): number
Returns the index of the first occurrence of a value in an array, or -1 if it is not present.
@param ― searchElement The value to locate in the array.
@param ― fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
indexOf("test");
if (
const index:number
index<0) {
// not found
}
const
const index:number
index =
const values:string[]
values.
Array<string>.indexOf(searchElement: string, fromIndex?: number): number
Returns the index of the first occurrence of a value in an array, or -1 if it is not present.
@param ― searchElement The value to locate in the array.
@param ― fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
indexOf("test");
if (
const index:number
index>=0) {
// found
}
const
const index:number
index =
const values:string[]
values.
Array<string>.indexOf(searchElement: string, fromIndex?: number): number
Returns the index of the first occurrence of a value in an array, or -1 if it is not present.
@param ― searchElement The value to locate in the array.
@param ― fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
indexOf("test");
if (
const index:number
index>-1) {
// found
}
const
const index:number
index =
const values:string[]
values.
Array<string>.indexOf(searchElement: string, fromIndex?: number): number
Returns the index of the first occurrence of a value in an array, or -1 if it is not present.
@param ― searchElement The value to locate in the array.
@param ― fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
indexOf("test");
if (
const index:number
index===-1) {
// not found
}
const
const index:number
index =
const values:string[]
values.
Array<string>.indexOf(searchElement: string, fromIndex?: number): number
Returns the index of the first occurrence of a value in an array, or -1 if it is not present.
@param ― searchElement The value to locate in the array.
@param ― fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
indexOf("test");
if (
const index:number
index!==-1) {
// found
}
if (
const values:number[]
values.
Array<number>.findIndex(predicate: (value:number, index:number, obj:number[])=> unknown, thisArg?: any): number
Returns the index of the first element in the array where predicate is true, and -1
otherwise.
@param ― predicate find calls predicate once for each element of the array, in ascending
order, until it finds one where predicate returns true. If such an element is found,
findIndex immediately returns that element index. Otherwise, findIndex returns -1.
@param ― thisArg If provided, it will be used as the this value for each invocation of
predicate. If it is not provided, undefined is used instead.
If your codebase has an established convention using < 0 or >= 0 for index checks, you may disable this rule.
Some developers find the comparison style more readable in certain contexts.