Lexical variables (let, const and class) must be declared before they are used

  • REFERENCE_BEFORE_LEXICAL_DECL
  • Error
  • High
  • es6

This rule applies when lexical variables to be declared with let, const or class are referenced before their declarations.

Referencing the variables before their declarations throws a ReferenceError exception.

Therefore, the lexical variables with let, const or class should be declared before they are referenced.

Noncompliant Code Example

View with compliant examples side by side
// Example 1
x = 42; // REFERENCE_BEFORE_LEXICAL_DECL alarm
let x = 43;

// Example 2
console.log(y); // REFERENCE_BEFORE_LEXICAL_DECL alarm
const y = 42;

// Example 3
new A(); // REFERENCE_BEFORE_LEXICAL_DECL alarm
class A {};

Compliant Code Example

View with noncompliant examples side by side
// Example 1
let x = 43;

// Example 2
const y = 42;
console.log(y);

// Example 3
class A {};
new A();

Version

This rule was introduced in DeepScan 1.0.0-alpha.

Was this documentation helpful?