Prefer using classList.toggle() over conditional classList.add() and classList.remove().
✅ This rule is included in the browser stylistic and stylisticStrict presets.
The classList.toggle() method provides a concise and expressive way to conditionally add or remove a CSS class.
Using conditional logic with classList.add() and classList.remove() creates unnecessary code duplication and makes the intent less clear.
This rule suggests replacing any if/else used to toggle a class name from a list with the equivalent toggle().
AE<53sp ansty="le--0:#D6DE">EB53sp>< (sp ansty="le--0:#D7DB">E0conditi (sp>EB)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#7FDBCA;--1:#096E">72eleme sp>A11classLiAB0a5<3(sp ansty="le--0:#D9F5DD;--1:#1111">13(sp><1"sp ansty="le--0:#ECC48D;--1:#984E">4Dacti1"sp>1<1"sp ansty="le--0:#D6DEEB;--1:#403F">531"sp> {d><);d ivclassNa="meec-li">53<} sp ansty="le--0:#C792EA;--1:#8844">AEel} sp>53);d>< {d ivclassNa="meec-li">nt< sp ansty="le--0:#7FDBCA;--1:#096E">72eleme sp>A11classLiAB0remo5<3(sp ansty="le--0:#D9F5DD;--1:#1111">13(sp><1"sp ansty="le--0:#ECC48D;--1:#984E">4Dacti1"sp>1<1"sp ansty="le--0:#D6DEEB;--1:#403F">531"sp> {d><);d ivclassNa="meec-li">5);d><3}d ivclassNa="meec-li">d3}d>AE<53sp ansty="le--0:#D6DE">EB53sp>< (sp ansty="le--0:#D7DB">E0isVisib (sp>EB)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#7FDBCA;--1:#096E">72butt sp>A11classLiAB0remo5<3(sp ansty="le--0:#D9F5DD;--1:#1111">13(sp><1"sp ansty="le--0:#ECC48D;--1:#984E">4Dhidd1"sp>1<1"sp ansty="le--0:#D6DEEB;--1:#403F">531"sp> {d><);d ivclassNa="meec-li">53<} sp ansty="le--0:#C792EA;--1:#8844">AEel} sp>53);d>< {d ivclassNa="meec-li">nt< sp ansty="le--0:#7FDBCA;--1:#096E">72butt sp>A11classLiAB0a5<3(sp ansty="le--0:#D9F5DD;--1:#1111">13(sp><1"sp ansty="le--0:#ECC48D;--1:#984E">4Dhidd1"sp>1<1"sp ansty="le--0:#D6DEEB;--1:#403F">531"sp> {d><);d ivclassNa="meec-li">5);d><3}d ivclassNa="meco"><}d>}d>3}d> 6E72ele4<4AE. spans="tyle--0:#FAF39F;--1:#11">1111class4AE.>4
<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0to4AE.>31<111" spans="tyle--0:#ECC48D;--1:#98">4E4Dac111">1<111" spans="tyle--1:#40"><3F53 spans="tyle--0:#D6">DE3F53>DBE0condiDE111">c6E72bu4<4AE. spans="tyle--0:#FAF39F;--1:#11">1111class4AE.>4
<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0to4AE.>31<111" spans="tyle--0:#ECC48D;--1:#98">4E4Dhi111">1<111" spans="tyle--0:#D6DEEB;--1:#40">3F111"><53, spans="tyle--0:#C792EA;--1:#88">453, ><4AE! spans="tyle--1:#40"><3F53 spans="tyle--0:#D7">DBE0isVis3F53>DE4AE!>
This rule is not configurable.
If you need to support very old browsers that do not implement the two-parameter version of classList.toggle() (Internet Explorer 11 and earlier), you might need to use the conditional add()/remove() pattern.
For modern browsers and applications, prefer toggle().