Lint Rules

useConst (since v11.0.0)

Require const declarations for variables that are never reassigned after declared.

Examples

Invalid

let a = 3;
console.log(a);
nursery/useConst.js:1:1 lint/nursery/useConst  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   This 'let' declares a variable which is never re-assigned.
  
  > 1 │ let a = 3;
   ^^^
    2 │ console.log(a);
    3 │ 
  
   'a' is never re-assigned.
  
  > 1 │ let a = 3;
       ^
    2 │ console.log(a);
    3 │ 
  
   Suggested fix: Use 'const' instead.
  
    1  - let·a·=·3;
      1+ const·a·=·3;
    2 2  console.log(a);
    3 3  
  
// `a` is redefined (not reassigned) on each loop step.
for (let a of [1, 2, 3]) {
    console.log(a);
}
nursery/useConst.js:2:6 lint/nursery/useConst  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   This 'let' declares a variable which is never re-assigned.
  
    1 │ // `a` is redefined (not reassigned) on each loop step.
  > 2 │ for (let a of [1, 2, 3]) {
        ^^^
    3 │     console.log(a);
    4 │ }
  
   'a' is never re-assigned.
  
    1 │ // `a` is redefined (not reassigned) on each loop step.
  > 2 │ for (let a of [1, 2, 3]) {
            ^
    3 │     console.log(a);
    4 │ }
  
   Suggested fix: Use 'const' instead.
  
    1 1  // `a` is redefined (not reassigned) on each loop step.
    2  - for·(let·a·of·[1,·2,·3])·{
      2+ for·(const·a·of·[1,·2,·3])·{
    3 3      console.log(a);
    4 4  }
  
// `a` is redefined (not reassigned) on each loop step.
for (let a in [1, 2, 3]) {
    console.log(a);
}
nursery/useConst.js:2:6 lint/nursery/useConst  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   This 'let' declares a variable which is never re-assigned.
  
    1 │ // `a` is redefined (not reassigned) on each loop step.
  > 2 │ for (let a in [1, 2, 3]) {
        ^^^
    3 │     console.log(a);
    4 │ }
  
   'a' is never re-assigned.
  
    1 │ // `a` is redefined (not reassigned) on each loop step.
  > 2 │ for (let a in [1, 2, 3]) {
            ^
    3 │     console.log(a);
    4 │ }
  
   Suggested fix: Use 'const' instead.
  
    1 1  // `a` is redefined (not reassigned) on each loop step.
    2  - for·(let·a·in·[1,·2,·3])·{
      2+ for·(const·a·in·[1,·2,·3])·{
    3 3      console.log(a);
    4 4  }
  
let a = 3;
{
    let a = 4;
    a = 2;
}
nursery/useConst.js:1:1 lint/nursery/useConst  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   This 'let' declares a variable which is never re-assigned.
  
  > 1 │ let a = 3;
   ^^^
    2 │ {
    3 │     let a = 4;
  
   'a' is never re-assigned.
  
  > 1 │ let a = 3;
       ^
    2 │ {
    3 │     let a = 4;
  
   Suggested fix: Use 'const' instead.
  
    1  - let·a·=·3;
      1+ const·a·=·3;
    2 2  {
    3 3      let a = 4;
  
let a = 1, b = 2;
b = 3;
nursery/useConst.js:1:1 lint/nursery/useConst ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   This 'let' declares a variable which is never re-assigned.
  
  > 1 │ let a = 1, b = 2;
   ^^^
    2 │ b = 3;
    3 │ 
  
   'a' is never re-assigned.
  
  > 1 │ let a = 1, b = 2;
       ^
    2 │ b = 3;
    3 │ 
  

Valid

let a = 2;
a = 3;
console.log(a);