Skip to content

noObjectConstructor (JavaScript)

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

Disallow calling Object without arguments to create an empty object.

Object() and new Object() without arguments both create an empty object, the same as {}. Writing {} is shorter and clearer, and it always creates a plain object, even if other code replaces the global Object.

Calls with an argument, such as Object(value), are allowed because they convert value to an object.

const obj = Object();
code-block.js:1:13 lint/nursery/noObjectConstructor  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Calling Object without arguments creates an empty object.

> 1 │ const obj = Object();
│ ^^^^^^^^
2 │

ℹ Writing {} is shorter, and it always creates a plain object, even if other code replaces the global Object.

ℹ 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.

ℹ Unsafe fix: Use an object literal.

1 │ - const·obj·=·Object();
1 │ + const·obj·=·{};
2 2 │

const obj = new Object();
code-block.js:1:13 lint/nursery/noObjectConstructor  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Calling Object without arguments creates an empty object.

> 1 │ const obj = new Object();
│ ^^^^^^^^^^^^
2 │

ℹ Writing {} is shorter, and it always creates a plain object, even if other code replaces the global Object.

ℹ 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.

ℹ Unsafe fix: Use an object literal.

1 │ - const·obj·=·new·Object();
1 │ + const·obj·=·{};
2 2 │

const obj = new Object;
code-block.js:1:13 lint/nursery/noObjectConstructor  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Calling Object without arguments creates an empty object.

> 1 │ const obj = new Object;
│ ^^^^^^^^^^
2 │

ℹ Writing {} is shorter, and it always creates a plain object, even if other code replaces the global Object.

ℹ 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.

ℹ Unsafe fix: Use an object literal.

1 │ - const·obj·=·new·Object;
1 │ + const·obj·=·{};
2 2 │

const obj = {};
const wrapped = Object(value);
const alsoWrapped = new Object(value);
// Here, `Object` is a parameter, not the built-in `Object`.
const createObject = (Object) => new Object();
  • To use [] instead of the Array constructor, see useArrayLiterals.
  • To require new when calling built-in constructors such as Object, see useConsistentBuiltinInstantiation. This rule reports both Object() and new Object() when they have no arguments.