Skip to content

arrayExistenceChecksConsistency

Reports inconsistent styles for checking element existence using index methods.

✅ This rule is included in the ts stylistic and stylisticStrict presets.

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.

AEconsB0indAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53valuAB0index5<3(sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4Dte1<1"sp ansty="le--0:#D6DEEB;--1:#403F">53<);d ivclassNa="meec-li">AE53 (indeA5<3 sp ansty="le--0:#F78C6C;--1:#AA09">8<20sp ansty="le--0:#D6DEEB;--1:#403F">53)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#919F9F;--1:#5D63">76// not fou5<3}d ivclassNa="meco"><}d>AEconsB0indAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53valuAB0index5<3(sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4Dte1<1"sp ansty="le--0:#D6DEEB;--1:#403F">53<);d ivclassNa="meec-li">AE53 (indeAE<>=sp ansty="le--0:#D6DEEB;--1:#403F">5=sp><3 sp ansty="le--0:#F78C6C;--1:#AA09">8<20sp ansty="le--0:#D6DEEB;--1:#403F">53)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#919F9F;--1:#5D63">76// fou5<3}d ivclassNa="meco"><}d>AEconsB0indAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53valuAB0index5<3(sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4Dte1<1"sp ansty="le--0:#D6DEEB;--1:#403F">53<);d ivclassNa="meec-li">AE53 (indeAsp ansty="le--0:#D6DEEB;--1:#403F">5sp><3 sp ansty="le--0:#C792EA;--1:#8844">A8<21sp ansty="le--0:#D6DEEB;--1:#403F">53)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#919F9F;--1:#5D63">76// fou5<3}d ivclassNa="meco"><}d>
44AEco61B0i44A3F53va4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0ind31<111" spans="tyle--0:#ECC48D;--1:#98">4E4D1<111" spans="tyle--0:#D6DEEB;--1:#40">3F<53) ;divclass="Nameec-">443F53 (in44A34<4AE- spans="tyle--0:#F78C6C;--1:#AA">0<9821 spans="tyle--0:#D6DEEB;--1:#40">3F5<3) {divclass="Nameec-">de6376// not f3<}di vdivclass="Nameexpressive-">44AEco61B0i44A3F53va4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0ind31<111" spans="tyle--0:#ECC48D;--1:#98">4E4D1<111" spans="tyle--0:#D6DEEB;--1:#40">3F<53) ;divclass="Nameec-">443F53 (in44A34<4AE- spans="tyle--0:#F78C6C;--1:#AA">0<9821 spans="tyle--0:#D6DEEB;--1:#40">3F5<3) {divclass="Nameec-">de6376// f3<}di vdivclass="Nameexpressive-">443F53 (va4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0findI31<111( spans="tyle--0:#D7DBE0;--1:#40">3F53v1<111) spans="tyle--0:#D6DEEB;--1:#40">344 spans="tyle--0:#D6DEEB;--1:#40">3F53 va>4<4AE> spans="tyle--0:#D6DEEB;--1:#40">3>09<8210 spans="tyle--0:#D6DEEB;--1:#40">3F<53) spans="tyle--0:#C792EA;--1:#88">44A34<4AE- spans="tyle--0:#F78C6C;--1:#AA">0<9821 spans="tyle--0:#D6DEEB;--1:#40">3F5<3) {divclass="Nameec-">de6376// not f3

This rule is not configurable.

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.