Skip to content

noInvalidUseBeforeDeclaration

biome.json
{
"linter": {
"rules": {
"correctness": {
"noInvalidUseBeforeDeclaration": "error"
}
}
}
}

Disallow the use of variables, function parameters, classes, and enums before their declaration

JavaScript doesn’t allow the use of block-scoped variables (let, const), function parameters, and classes before their declaration. Similarly TypeScript doesn’t allow the use of enums before their declaration. A ReferenceError will be thrown with any attempt to access the variable or the parameter before its declaration.

The rule also reports the use of variables declared with var before their declarations.

function f() {
console.log(x);
let x;
}
code-block.js:2:17 lint/correctness/noInvalidUseBeforeDeclaration ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This variable is used before its declaration.

1 │ function f() {
> 2 │ console.log(x);
^
3 │ let x;
4 │ }

The variable is declared here:

1 │ function f() {
2 │ console.log(x);
> 3 │ let x;
^
4 │ }
5 │

function f() {
console.log(x);
var x = 0;
}
code-block.js:2:17 lint/correctness/noInvalidUseBeforeDeclaration ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This variable is used before its declaration.

1 │ function f() {
> 2 │ console.log(x);
^
3 │ var x = 0;
4 │ }

The variable is declared here:

1 │ function f() {
2 │ console.log(x);
> 3 │ var x = 0;
^
4 │ }
5 │

function f(a = b, b = 0) {}
code-block.js:1:16 lint/correctness/noInvalidUseBeforeDeclaration ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This parameter is used before its declaration.

> 1 │ function f(a = b, b = 0) {}
^
2 │

The parameter is declared here:

> 1 │ function f(a = b, b = 0) {}
^
2 │

new C();
class C {}
code-block.js:1:5 lint/correctness/noInvalidUseBeforeDeclaration ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This class is used before its declaration.

> 1 │ new C();
^
2 │ class C {}
3 │

The class is declared here:

1 │ new C();
> 2 │ class C {}
^
3 │

f();
function f() {}
// An export can reference a variable before its declaration.
export { CONSTANT };
const CONSTANT = 0;
function f() { return CONSTANT; }
const CONSTANT = 0;
function f() {
new C();
}
let c: C;
class C {}