Skip to content
← Back to rules

unicorn/no-new-array Correctness

✅ This rule is turned on by default.
⚠️ 💡 A dangerous suggestion is available for this rule.

What it does ​

Disallow new Array().

Why is this bad? ​

When using the Array constructor with one argument, it's not clear whether the argument is meant to be the length of the array or the only element.

Examples ​

Examples of incorrect code for this rule:

javascript
const array = new Array(1);
const array = new Array(42);
const array = new Array(foo);

Examples of correct code for this rule:

javascript
const array = Array.from({ length: 42 });
const array = [42];

How to use ​

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/no-new-array": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/no-new-array": "error",
  },
});
bash
oxlint --deny unicorn/no-new-array

Version ​

This rule was added in v0.0.16.

References ​