# prefer-immediate-return

Rule `sonarjs/prefer-immediate-return`. Local variables should not be declared and then immediately returned or thrown

- Source: sonarjs
- Technologies: JavaScript
- Kind: style
- Needs type information: yes
- Autofix: yes, in at least one linter
- Reach: 0 weekly downloads of presets that enable it
- Trend: 0% last quarter
- Rank: #2173 by reach

## Linters

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

## Code examples

### Incorrect: the rule reports this

```js
function computeDurationInMilliseconds(hours, minutes, seconds) {
  let duration = (((hours * 60) + minutes) * 60 + seconds ) * 1000;
  return duration;
}
```

### Correct: the rule accepts this

```js
function computeDurationInMilliseconds(hours, minutes, seconds) {
  return (((hours * 60) + minutes) * 60 + seconds ) * 1000;
}
```

Examples from the ESLint documentation.

## Enable it

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

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

## Related rules

- [sonarjs/prefer-object-literal](https://lintradar.dev/rules/sonarjs/prefer-object-literal.md): Object literal syntax should be used
- [sonarjs/operation-returning-nan](https://lintradar.dev/rules/sonarjs/operation-returning-nan.md): Arithmetic operations should not result in "NaN"
- [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/non-number-in-arithmetic-expression](https://lintradar.dev/rules/sonarjs/non-number-in-arithmetic-expression.md): Arithmetic operators should only have numbers as operands
- [sonarjs/strings-comparison](https://lintradar.dev/rules/sonarjs/strings-comparison.md): Comparison operators should not be used with strings
- [sonarjs/no-wildcard-import](https://lintradar.dev/rules/sonarjs/no-wildcard-import.md): Wildcard imports should not be used

---

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