# prefer-object-literal

Rule `sonarjs/prefer-object-literal`. Object literal syntax should be used

- Source: sonarjs
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: no
- Reach: 0 weekly downloads of presets that enable it
- Trend: 0% last quarter
- Rank: #2174 by reach

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/prefer-object-literal` | no | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-2428/) |

## Code examples

### Incorrect: the rule reports this

```js
let person = {};  // Noncompliant
person.firstName = "John";
person.middleInitial = "Q";
person.lastName = "Public";
```

### Correct: the rule accepts this

```js
let person = {
  firstName: "John",
  middleInitial: "Q",
  lastName: "Public",
}
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
export default [
  {
    rules: {
      "sonarjs/prefer-object-literal": "error",
    },
  },
];
```

## Related rules

- [sonarjs/prefer-immediate-return](https://lintradar.dev/rules/sonarjs/prefer-immediate-return.md): Local variables should not be declared and then immediately returned or thrown
- [sonarjs/shorthand-property-grouping](https://lintradar.dev/rules/sonarjs/shorthand-property-grouping.md): Shorthand object properties should be grouped at the beginning or end of an object declaration
- [sonarjs/strings-comparison](https://lintradar.dev/rules/sonarjs/strings-comparison.md): Comparison operators should not be used with strings
- [sonarjs/operation-returning-nan](https://lintradar.dev/rules/sonarjs/operation-returning-nan.md): Arithmetic operations should not result in "NaN"
- [sonarjs/too-many-break-or-continue-in-loop](https://lintradar.dev/rules/sonarjs/too-many-break-or-continue-in-loop.md): Loops should not contain more than a single "break" or "continue" statement
- [sonarjs/non-number-in-arithmetic-expression](https://lintradar.dev/rules/sonarjs/non-number-in-arithmetic-expression.md): Arithmetic operators should only have numbers as operands

---

Web page: https://lintradar.dev/rules/sonarjs/prefer-object-literal. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
