Skip to content

Files

Latest commit

 

History

History
40 lines (30 loc) · 741 Bytes

no-eq-null.md

File metadata and controls

40 lines (30 loc) · 741 Bytes

Pattern: Loose equality comparison with null

Issue: -

Description

Using loose equality operators (==, !=) when comparing with null can produce unexpected results because the comparison will match both null and undefined. Use strict equality operators (===, !==) for explicit null checks.

Examples

Example of incorrect code:

if (foo == null) {
  handleNull();
}

if (foo != null) {
  handleNotNull();
}

while (value == null) {
  waitForValue();
}

Example of correct code:

if (foo === null) {
  handleNull();
}

if (foo !== null) {
  handleNotNull();
}

// Check for both null and undefined explicitly
if (foo == null && foo === undefined) {
  handleNullOrUndefined();
}