Skip to content

oxc/bad-object-literal-comparison Correctness

This rule is turned on by default.

What it does

Checks for comparisons between object and array literals.

Why is this bad?

Comparing a variable to an object or array literal will always return false as object and array literals are never equal to each other.

If you want to check if an object or array is empty, use Object.entries() or Object.keys() and their lengths.

Example

Examples of incorrect code for this rule:

javascript
if (x === {}) {
}
if (arr !== []) {
}

Examples of correct code for this rule:

javascript
if (typeof x === "object" && Object.keys(x).length === 0) {
}
if (Array.isArray(x) && x.length === 0) {
}

References

Released under the MIT License.