# no-nested-switch

Rule `sonarjs/no-nested-switch`. "switch" statements should not be nested

- 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: #1855 by reach

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/no-nested-switch` | no | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-1821/) |
| Biome | `nursery/noNestedSwitch` | no | no | group nursery, since 2.6.0 | [docs](https://biomejs.dev/linter/rules/no-nested-switch/) |

## Code examples

### Incorrect: the rule reports this

```js
function foo(n, m) {
  switch (n) {
    case 0:
      switch (m) {  // Noncompliant; nested switch
        // ...
      }
    case 1:
      // ...
    default:
      // ...
  }
}
```

### Correct: the rule accepts this

```js
function foo(n, m) {
  switch (n) {
    case 0:
      bar(m);
    case 1:
      // ...
    default:
      // ...
  }
}

function bar(m) {
  switch(m) {
    // ...
  }
}
```

Examples from the ESLint documentation.

## Enable it

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

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

### Biome: `biome.json`

```json
{
  "linter": {
    "rules": {
      "nursery": {
        "noNestedSwitch": "error"
      }
    }
  }
}
```

## Related rules

- [sonarjs/arguments-usage](https://lintradar.dev/rules/sonarjs/arguments-usage.md): "arguments" should not be accessed directly
- [sonarjs/array-constructor](https://lintradar.dev/rules/sonarjs/array-constructor.md): Array constructors should not be used
- [sonarjs/arrow-function-convention](https://lintradar.dev/rules/sonarjs/arrow-function-convention.md): Braces and parentheses should be used consistently with arrow functions
- [sonarjs/aws-iam-all-resources-accessible](https://lintradar.dev/rules/sonarjs/aws-iam-all-resources-accessible.md): IAM policies should not grant access to all account resources
- [sonarjs/bool-param-default](https://lintradar.dev/rules/sonarjs/bool-param-default.md): Optional boolean parameters should have default value
- [sonarjs/class-prototype](https://lintradar.dev/rules/sonarjs/class-prototype.md): Class methods should be used instead of "prototype" assignments

---

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