Reassignment should not be made to a variable that is declared using const keyword

  • BAD_ASSIGN_TO_CONST
  • Error
  • High
  • es6

This rule applies when reassignment is made to a variable declared using const keyword.

The value of the const variable is constant in block-scope. So TypeError exception occurs when the const variable is reassigned in block-scope.

Noncompliant Code Example

const A = 1;
A = 2; // BAD_ASSIGN_TO_CONST alarm because A is reserved for constant above.
const MY_OBJECT = {'key': 'value'};
MY_OBJECT = {'oKey': 'otherValue'}; // BAD_ASSIGN_TO_CONST alarm
const MY_ARRAY = [];
MY_ARRAY = ["A"]; // BAD_ASSIGN_TO_CONST alarm

Compliant Code Example

const MY_OBJECT = {'key': 'value'};
MY_OBJECT.key = "otherValue";
const MY_ARRAY = [];
MY_ARRAY.push("B");

Version

This rule was introduced in DeepScan 1.0.0-alpha.

See