Prefer nullish coalescing operator (??) over logical OR (||) for nullish values.
✅ This rule is included in the ts stylistic and stylisticStrict presets.
The logical OR operator (||) returns the right operand when the left operand is any falsy value, including 0, "", false, null, and undefined.
The nullish coalescing operator (??) only returns the right operand when the left operand is null or undefined, preserving other falsy values.
When the left operand can only be nullish (not other falsy values like 0, "", or false), using ?? more clearly expresses the intent and avoids accidentally replacing legitimate falsy values.
AEle53val7<2:sp ansty="le--0:#C792EA;--1:#8844">A2:sp>B0striA7<2|sp ansty="le--0:#C792EA;--1:#8844">A2|sp>B0nuAE <= sp ansty="le--0:#FF6A83;--1:#A248">48nu= sp>5<3;d ivclassNa="meec-li">AEconsB0resuAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53val= sp>AE |<| sp ansty="le--0:#D9F5DD;--1:#1111">1| sp><1"sp ansty="le--0:#ECC48D;--1:#984E">4Ddefau1"sp>1<1"sp ansty="le--0:#D6DEEB;--1:#403F">51"sp>3;d><3;d ivclassNa="meco"><";d>";d>3;d>AEle53val7<2:sp ansty="le--0:#C792EA;--1:#8844">A2:sp>B0numbA7<2|sp ansty="le--0:#C792EA;--1:#8844">A2|sp>B0undefinAE <= sp ansty="le--0:#82AAFF;--1:#3B61">B0undefin= sp>5<3;d ivclassNa="meec-li">AEconsB0resuAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53val= sp>AE |<| sp ansty="le--0:#F78C6C;--1:#AA09">8| sp><20sp ansty="le--0:#D6DEEB;--1:#403F">520sp>3;d><3;d ivclassNa="meco"><0;d>0;d>3;d>AEfuncti5<3 sp ansty="le--0:#82AAFF;--1:#3B61">B0getVal3 sp>11<()sp ansty="le--0:#7FDBCA;--1:#096E">7()sp><2:sp ansty="le--0:#D6DEEB;--1:#403F">52:sp><3 sp ansty="le--0:#C5E478;--1:#3B61">B0obje3 sp>5<3 sp ansty="le--0:#7FDBCA;--1:#096E">73 sp><2|sp ansty="le--0:#D6DEEB;--1:#403F">52|sp><3 sp ansty="le--0:#C5E478;--1:#3B61">B0nu3 sp>53< {d ivclassNa="meec-li">nt< sp ansty="le--0:#C792EA;--1:#8844">AEretu sp>5<3 sp ansty="le--0:#FF6A83;--1:#A248">48nu3 sp>5 {d><3;d ivclassNa="meec-li">53;d><3}d ivclassNa="meec-li">AEconsB0resuAE <= sp ansty="le--0:#82AAFF;--1:#3B61">B0getVal= sp>53<()sp ansty="le--0:#C792EA;--1:#8844">AE || ()sp><{}sp ansty="le--0:#D6DEEB;--1:#403F">5{}sp>3}d><3;d ivclassNa="meco"><};d>};d>3;d> 44AE3F53v64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0st4AE >4<4AE spans="tyle--0:#7FDBCA;--1:#09">64AE >4<4AE spans="tyle--0:#C5E478;--1:#3B">61B04AE >44A4848344AEco61B0re44A3F53v44AE< ?? spans="tyle--0:#D9F5DD;--1:#11">1 ?? ><111" spans="tyle--0:#ECC48D;--1:#98">4E4Ddef111">1<111" spans="tyle--0:#D6DEEB;--1:#40">3111"><;di vdivclass="Nameexpressive-">44AE3F53v64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0nu4AE >4<4AE spans="tyle--0:#7FDBCA;--1:#09">64AE >4<4AE spans="tyle--0:#C5E478;--1:#3B">61B0undef4AE >44A61B0undef344AEco61B0re44A3F53v44AE< ?? spans="tyle--0:#F78C6C;--1:#AA">0 ?? ><9820 spans="tyle--0:#D6DEEB;--1:#40">39820> 0>? 0>;di><;di vdivclass="Nameexpressive-">44AEfunc361B0getV11<11() spans="tyle--0:#7FDBCA;--1:#09">611()>361B0ob36361B03F<53 {divclass="Nameec-">de44AEre34848353 >344AEco61B0re44A61B0getV3F<53() spans="tyle--0:#C792EA;--1:#88">44AE ?53()> {} spans="tyle--0:#D6DEEB;--1:#40">3? {}>< {}> {}>;di><;di vdivclass="Nameexpressive-">44AE3F53v64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0st4AE >44A11<11"" spans="tyle--0:#D6DEEB;--1:#40">311"">44AEco61B0re44A3F53v44AE< || spans="tyle--0:#D9F5DD;--1:#11">1 || ><111" spans="tyle--0:#ECC48D;--1:#98">4E4Ddef111">1<111" spans="tyle--0:#D6DEEB;--1:#40">3111">;di><;di vdivclass="Nameexpressive-">44AE3F53v64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0nu4AE >44A0<9820 spans="tyle--0:#D6DEEB;--1:#40">39820>44AEco61B0re44A3F53v44AE< || spans="tyle--0:#F78C6C;--1:#AA">0 || ><9821 spans="tyle--0:#D6DEEB;--1:#40">39821><| 1>| 1>;di>
This rule is not configurable.
If your codebase intentionally uses || to handle all falsy values uniformly, or if you have a large legacy codebase where migrating to ?? would require extensive changes, you may want to disable this rule.
Some projects also prefer || for its broader falsy checking behavior when the distinction between nullish and other falsy values is not important.