# no-small-switch

Rule `sonarjs/no-small-switch`. "if" statements should be preferred over "switch" when simpler

- Source: sonarjs
- Technologies: JavaScript
- Kind: style
- Needs type information: yes
- Autofix: no
- Reach: 6.2M weekly downloads of presets that enable it
- Trend: +62.1% last quarter
- Rank: #1317 by reach, #618 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/no-small-switch` | yes | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-1301/) |

## Code examples

### Incorrect: the rule reports this

```js
switch (variable) {
  case 0:
    doSomething();
    break;
  default:
    doSomethingElse();
    break;
}
```

### Correct: the rule accepts this

```js
if (variable == 0) {
  doSomething();
} else {
  doSomethingElse();
}
```

Examples from the ESLint documentation.

## Enable it

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

```js
export default [
  {
    rules: {
      "sonarjs/no-small-switch": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [SonarJS recommended](https://lintradar.dev/presets/sonarjs-recommended.md) | ESLint | `eslint-plugin-sonarjs` | 230 | 6.2M | +62.1% |

## Related rules

- [sonarjs/no-skipped-tests](https://lintradar.dev/rules/sonarjs/no-skipped-tests.md): Tests should not be skipped without providing a reason
- [sonarjs/no-table-as-layout](https://lintradar.dev/rules/sonarjs/no-table-as-layout.md): HTML "<table>" should not be used for layout purposes
- [sonarjs/no-session-cookies-on-static-assets](https://lintradar.dev/rules/sonarjs/no-session-cookies-on-static-assets.md): Static Assets should not serve session cookies
- [sonarjs/no-trivial-assertions](https://lintradar.dev/rules/sonarjs/no-trivial-assertions.md): Assertions should not be trivially true
- [sonarjs/no-selector-parameter](https://lintradar.dev/rules/sonarjs/no-selector-parameter.md): Methods should not contain selector parameters
- [sonarjs/no-try-promise](https://lintradar.dev/rules/sonarjs/no-try-promise.md): Promise rejections should not be caught by "try" blocks

---

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