Skip to content

Files

Latest commit

 

History

History
31 lines (23 loc) · 599 Bytes

bad-object-literal-comparison.md

File metadata and controls

31 lines (23 loc) · 599 Bytes

Pattern: Object literal comparison

Issue: -

Description

Comparing a variable directly with an object or array literal will always return false because each literal creates a new reference. Use appropriate methods to check object or array properties instead.

Examples

Example of incorrect code:

if (obj === {}) {
  // Always false
}

if (array !== []) {
  // Always true
}

Example of correct code:

if (Object.keys(obj).length === 0) {
  // Properly checks if object is empty
}

if (array.length === 0) {
  // Properly checks if array is empty
}