Skip to content

Files

Latest commit

 

History

History
27 lines (19 loc) · 548 Bytes

bad-bitwise-operator.md

File metadata and controls

27 lines (19 loc) · 548 Bytes

Pattern: Bitwise operator misuse

Issue: -

Description

Using bitwise operators (& |) where logical operators (&& ||) are expected can cause bugs. Bitwise operators don't short-circuit and operate on individual bits rather than boolean values.

Examples

Example of incorrect code:

if (obj & obj.property) {
  console.log(obj.property);
}

const value = input | defaultValue;

Example of correct code:

if (obj && obj.property) {
  console.log(obj.property);
}

const value = input || defaultValue;