# cognitive-complexity

Rule `sonarjs/cognitive-complexity`. Cognitive Complexity of functions should not be too high

- Source: sonarjs
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: no
- Reach: 8.3M weekly downloads of presets that enable it
- Trend: +77.8% last quarter
- Rank: #1141 by reach, #450 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/cognitive-complexity` | yes | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-3776/) |
| Biome | `complexity/noExcessiveCognitiveComplexity` | no | no | group complexity, since 1.0.0 | [docs](https://biomejs.dev/linter/rules/no-excessive-cognitive-complexity/) |

## Code examples

### Incorrect: the rule reports this

```js
function tooComplex() {
    for (let x = 0; x < 10; x++) {
        for (let y = 0; y < 10; y++) {
            for (let z = 0; z < 10; z++) {
                if (x % 2 === 0) {
                    if (y % 2 === 0) {
                        console.log(x > y ? `${x} > ${y}` : `${y} > ${x}`);
                    }
                }
            }
        }
    }
}
```

Examples from the Biome documentation.

## Enable it

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

```js
export default [
  {
    rules: {
      "sonarjs/cognitive-complexity": "error",
    },
  },
];
```

### Biome: `biome.json`

```json
{
  "linter": {
    "rules": {
      "complexity": {
        "noExcessiveCognitiveComplexity": "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% |
| [Ultracite for Biome](https://lintradar.dev/presets/ultracite-biome.md) | Biome | `ultracite` | 616 | 2.1M | +151.7% |

## Related rules

- [sonarjs/prefer-while](https://lintradar.dev/rules/sonarjs/prefer-while.md): A "while" loop should be used instead of a "for" loop
- [sonarjs/no-nested-template-literals](https://lintradar.dev/rules/sonarjs/no-nested-template-literals.md): Template literals should not be nested
- [sonarjs/anchor-precedence](https://lintradar.dev/rules/sonarjs/anchor-precedence.md): Alternatives in regular expressions should be grouped when used with anchors
- [sonarjs/argument-type](https://lintradar.dev/rules/sonarjs/argument-type.md): Arguments to built-in functions should match documented types
- [sonarjs/arguments-order](https://lintradar.dev/rules/sonarjs/arguments-order.md): Parameters should be passed in the correct order
- [sonarjs/array-callback-without-return](https://lintradar.dev/rules/sonarjs/array-callback-without-return.md): Callbacks of array methods should have return statements

---

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