Skip to content

noObjectLiteralComparison (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noObjectLiteralComparison": "error"
}
}
}
}

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.

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) {}