Pattern: Bitwise operator misuse
Issue: -
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.
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;