Lint Rules

noConstAssign (since v10.0.0)

Prevents from having const variables being re-assigned.

Trying to assign a value to a const will cause an TypeError when the code is executed.

Examples

Invalid

const a = 1;
a = 4;
nursery/noConstAssign.js:2:1 lint/nursery/noConstAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   Can't assign a because it's a constant
  
    1 │ const a = 1;
  > 2 │ a = 4;
   ^
    3 │ 
  
   This is where the variable is defined as constant
  
  > 1 │ const a = 1;
         ^
    2 │ a = 4;
    3 │ 
  
const a = 2;
a += 1;
nursery/noConstAssign.js:2:1 lint/nursery/noConstAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   Can't assign a because it's a constant
  
    1 │ const a = 2;
  > 2 │ a += 1;
   ^
    3 │ 
  
   This is where the variable is defined as constant
  
  > 1 │ const a = 2;
         ^
    2 │ a += 1;
    3 │ 
  
const a = 1;
++a;
nursery/noConstAssign.js:2:3 lint/nursery/noConstAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   Can't assign a because it's a constant
  
    1 │ const a = 1;
  > 2 │ ++a;
     ^
    3 │ 
  
   This is where the variable is defined as constant
  
  > 1 │ const a = 1;
         ^
    2 │ ++a;
    3 │ 
  
const a = 1, b = 2;

a = 2;
nursery/noConstAssign.js:3:1 lint/nursery/noConstAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   Can't assign a because it's a constant
  
    1 │ const a = 1, b = 2;
    2 │ 
  > 3 │ a = 2;
   ^
    4 │ 
  
   This is where the variable is defined as constant
  
  > 1 │ const a = 1, b = 2;
         ^
    2 │ 
    3 │ a = 2;
  

Valid

const a = 10;
let b = 10;
b = 20;