noObjectLiteralComparison (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.5 - Diagnostic Category:
lint/nursery/noObjectLiteralComparison - This rule doesn’t have a fix.
- The default severity of this rule is error.
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noObjectLiteralComparison": "error" } } }}Description
Section titled “Description”Disallow comparing a value with an empty object {} or an empty array [].
Code like value === {} or list === [] looks like it checks whether a value is empty, but it doesn’t.
Each {} and [] creates a new object, and JavaScript compares objects and arrays by reference,
not by their contents. With ===, the comparison is always false, and with !==, it’s always true.
With == and !=, JavaScript first converts {} to the string "[object Object]" and [] to the
empty string "" when the other value isn’t an object, so "" == [] and 0 == [] are both true,
even though neither value is an empty array.
To check whether an object has no properties, use Object.keys(value).length === 0.
To check whether an array is empty, use value.length === 0.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”if (value === {}) {}code-block.js:1:5 lint/nursery/noObjectLiteralComparison ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This compares a value with an empty object literal.
> 1 │ if (value === {}) {}
│ ^^^^^^^^^^^^
2 │
ℹ Each object literal creates a new object. Comparing a value with it never checks whether the value is empty: objects and arrays are compared by reference, not by their contents.
ℹ To check whether an object has no properties, use Object.keys(value).length === 0.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
if (list !== []) {}code-block.js:1:5 lint/nursery/noObjectLiteralComparison ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This compares a value with an empty array literal.
> 1 │ if (list !== []) {}
│ ^^^^^^^^^^^
2 │
ℹ Each array literal creates a new array. Comparing a value with it never checks whether the value is empty: objects and arrays are compared by reference, not by their contents.
ℹ To check whether an array is empty, use value.length === 0.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
if (value == {}) {}code-block.js:1:5 lint/nursery/noObjectLiteralComparison ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This compares a value with an empty object literal.
> 1 │ if (value == {}) {}
│ ^^^^^^^^^^^
2 │
ℹ Each object literal creates a new object. Comparing a value with it never checks whether the value is empty: objects and arrays are compared by reference, not by their contents.
ℹ To check whether an object has no properties, use Object.keys(value).length === 0.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
if (value !== null && typeof value === "object" && Object.keys(value).length === 0) {}if (Array.isArray(list) && list.length === 0) {}if (value === null) {}See Also
Section titled “See Also”- To report more comparisons that always give the same result, see
noConstantBinaryExpressions.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.