# no-function-declaration-in-block

Rule `sonarjs/no-function-declaration-in-block`. Function declarations should not be made within blocks

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

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/no-function-declaration-in-block` | no | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-1530/) |

## Code examples

### Incorrect: the rule reports this

```js
if (x) {
  function foo() {} //foo is hoisted in Chrome, Firefox and Safari, but not in Edge.
}
```

### Correct: the rule accepts this

```js
if (x) {
  const foo = function() {}
}
```

Examples from the ESLint documentation.

## Enable it

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

```js
export default [
  {
    rules: {
      "sonarjs/no-function-declaration-in-block": "error",
    },
  },
];
```

## Related rules

- [sonarjs/no-for-in-iterable](https://lintradar.dev/rules/sonarjs/no-for-in-iterable.md): "for in" should not be used with iterables
- [sonarjs/no-implicit-dependencies](https://lintradar.dev/rules/sonarjs/no-implicit-dependencies.md): Dependencies should be explicit
- [sonarjs/no-empty-test-file](https://lintradar.dev/rules/sonarjs/no-empty-test-file.md): Test files should contain at least one test case
- [sonarjs/no-inconsistent-returns](https://lintradar.dev/rules/sonarjs/no-inconsistent-returns.md): Functions should use "return" consistently
- [sonarjs/no-duplicate-string](https://lintradar.dev/rules/sonarjs/no-duplicate-string.md): String literals should not be duplicated
- [sonarjs/no-incorrect-string-concat](https://lintradar.dev/rules/sonarjs/no-incorrect-string-concat.md): Strings and non-strings should not be added

---

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