Skip to content

negativeZeroComparisons

Reports comparisons with -0 that may not behave as expected.

✅ This rule is included in the ts logical and logicalStrict presets.

In JavaScript, -0 and +0 are distinct values, but standard comparison operators (===, ==, !==, !=, <, <=, >, >=) treat them as equal. This can lead to unexpected behavior when you need to distinguish between these values.

The Object.is() method correctly distinguishes between -0 and +0, making it the reliable choice when checking for negative zero.

AEfuncti5<3 sp ansty="le--0:#82AAFF;--1:#3B61">B0checkVal1<1(sp ansty="le--0:#D7DBE0;--1:#403F">53val7<2:sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#C5E478;--1:#3B61">B0numb1<1)sp ansty="le--0:#D6DEEB;--1:#403F">53< {d ivclassNa="meec-li">nt< sp ansty="le--0:#C792EA;--1:#8844">AE53 (valuAE=<==sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#C792EA;--1:#8844">A8<20sp ansty="le--0:#D6DEEB;--1:#403F">53)< {d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEretu5<3 sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4Dnegative ze1<1"sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meec-li">53< sp ansty="le--0:#D6DEEB;--1:#403F">5<3}d ivclassNa="meec-li">nt< sp ansty="le--0:#C792EA;--1:#8844">AEretu5<3 sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4Doth1<1"sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meec-li">5<3}d ivclassNa="meec-li">d76// Also incorrect with other operatoAE53 (valuAE<==sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#C792EA;--1:#8844">A8<20sp ansty="le--0:#D6DEEB;--1:#403F">53)< {d ivclassNa="meec-li">5<3}d ivclassNa="meec-li">AE53 (valuAE!<==sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#C792EA;--1:#8844">A8<20sp ansty="le--0:#D6DEEB;--1:#403F">53)< {d ivclassNa="meec-li">5<3}d ivclassNa="meec-li">AE53 (valuA5<3 sp ansty="le--0:#C792EA;--1:#8844">A8<20sp ansty="le--0:#D6DEEB;--1:#403F">53)< {d ivclassNa="meec-li">5<3}d ivclassNa="meec-li">AE53 (valuAE<>=sp ansty="le--0:#D6DEEB;--1:#403F">5=sp><3 sp ansty="le--0:#C792EA;--1:#8844">A8<20sp ansty="le--0:#D6DEEB;--1:#403F">53)< {d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>
44AEfunc361B0checkV1<111( spans="tyle--0:#D7DBE0;--1:#40">3F53v6361B0nu1<111) spans="tyle--0:#D6DEEB;--1:#40">3F<53 {divclass="Nameec-">de443F53 (Ob4<4AE. spans="tyle--0:#82AAFF;--1:#3B">613F53(v6<67B, spans="tyle--0:#D6DEEB;--1:#40">34<4AE- spans="tyle--0:#F78C6C;--1:#AA">0<9820 spans="tyle--0:#D6DEEB;--1:#40">3F53<)) {divclass="Nameec-">dent< spans="tyle--0:#C792EA;--1:#88">44AEre31<111" spans="tyle--0:#ECC48D;--1:#98">4E4Dnegative 1<111" spans="tyle--0:#D6DEEB;--1:#40">33F<53 spans="tyle--0:#D6DEEB;--1:#40">3de44AEre31<111" spans="tyle--0:#ECC48D;--1:#98">4E4Do1<111" spans="tyle--0:#D6DEEB;--1:#40">33c6376// Regular zero comparisons are 443F53 (va44A30<9820 spans="tyle--0:#D6DEEB;--1:#40">3F5<3) {divclass="Nameec-">3c6376// Negative one and other numbers are 443F53 (va44A34<4AE- spans="tyle--0:#F78C6C;--1:#AA">0<9821 spans="tyle--0:#D6DEEB;--1:#40">3F5<3) {divclass="Nameec-">3c6376// Using -0 in non-comparison contexts is 44AEco61B0negative44AE< = - spans="tyle--0:#F78C6C;--1:#AA">0<9820 spans="tyle--0:#D6DEEB;--1:#40">344AEco61B0re44AE< = - spans="tyle--0:#F78C6C;--1:#AA">0<9820 spans="tyle--0:#C792EA;--1:#88">44A0<9821 spans="tyle--0:#D6DEEB;--1:#40">3<+ 1>

This rule is not configurable.

If you never need to distinguish between -0 and +0 in your codebase, you might choose to disable this rule. However, keeping it enabled helps prevent subtle bugs where the distinction matters, such as when working with mathematical calculations that produce negative zero (e.g., -1 / Infinity results in -0).