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 a TypeError
exception occurs when the const
variable is reassigned in block-scope.
This rule also applies to using
and await using
variables for explicit resource management because they are also read-only.
Noncompliant Code Example
View with compliant examples side by side// Example 1
const A = 1;
A = A + 1; // BAD_ASSIGN_TO_CONST alarm
// Example 2
const MY_OBJECT = { key1: 'value1' };
MY_OBJECT = { key2: 'value2' }; // BAD_ASSIGN_TO_CONST alarm
// Example 3
const MY_ARRAY = [];
MY_ARRAY = ["A"]; // BAD_ASSIGN_TO_CONST alarm
Compliant Code Example
View with noncompliant examples side by side// Example 1
let A = 1;
A = A + 1;
// Example 2
const MY_OBJECT = { key1: 'value1' };
MY_OBJECT.key2 = "value2";
// Example 3
const MY_ARRAY = [];
MY_ARRAY.push("A");
Version
This rule was introduced in DeepScan 1.0.0-alpha.