Skip to content

Avoid-Function-Declarations-In-Blocks

Documentation Index

Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt

Use this file to discover all available pages before exploring further.

Sourcery rule id: avoid-function-declarations-in-blocks

Section titled “Sourcery rule id: avoid-function-declarations-in-blocks”

Avoid function declarations, favouring function assignment expressions, inside blocks.

if (x) {
function foo() {}
}

Function declarations may be hoisted in Javascript, but the behaviour is inconsistent between browsers. Hoisting is generally confusing and should be avoided. Rather than using function declarations inside blocks, you should use function expressions, which create functions in-scope.