Skip to content

noUselessUndefinedInitialization (since v1.7.2)

Diagnostic Category: lint/nursery/noUselessUndefinedInitialization

Sources:

Disallow initializing variables to undefined.

A variable that is declared and not initialized to any value automatically gets the value of undefined. It’s considered a best practice to avoid initializing variables to undefined. Please note that any inline comments attached to the initialization value or variable will be removed on auto-fix. Please be also aware that this differs from Eslint’s behaviour and we are still discussing on how to properly handle this case.

var a = undefined;
nursery/noUselessUndefinedInitialization.js:1:7 lint/nursery/noUselessUndefinedInitialization  FIXABLE  ━━━━━━━━━━

   It's not necessary to initialize a to undefined.
  
  > 1 │ var a = undefined;
         ^^^^^^^^^^^
    2 │ 
  
   A variable that is declared and not initialized to any value automatically gets the value of undefined.
  
   Unsafe fix: Remove undefined initialization.
  
    1 │ var·a·=·undefined;
        ----------- 
let b = undefined, c = 1, d = 2;
nursery/noUselessUndefinedInitialization.js:1:7 lint/nursery/noUselessUndefinedInitialization  FIXABLE  ━━━━━━━━━━

   It's not necessary to initialize b to undefined.
  
  > 1 │ let b = undefined, c = 1, d = 2;
         ^^^^^^^^^^^
    2 │ 
  
   A variable that is declared and not initialized to any value automatically gets the value of undefined.
  
   Unsafe fix: Remove undefined initialization.
  
    1 │ let·b·=·undefined,·c·=·1,·d·=·2;
        -----------               
for (let i = 0; i < 100; i++) {
let i = undefined;
}
nursery/noUselessUndefinedInitialization.js:2:8 lint/nursery/noUselessUndefinedInitialization  FIXABLE  ━━━━━━━━━━

   It's not necessary to initialize i to undefined.
  
    1 │ for (let i = 0; i < 100; i++) {
  > 2 │ 	let i = undefined;
   	      ^^^^^^^^^^^
    3 │ }
    4 │ 
  
   A variable that is declared and not initialized to any value automatically gets the value of undefined.
  
   Unsafe fix: Remove undefined initialization.
  
    2 │ let·i·=·undefined;
          ----------- 
let f = /**/undefined/**/ ;
nursery/noUselessUndefinedInitialization.js:1:7 lint/nursery/noUselessUndefinedInitialization  FIXABLE  ━━━━━━━━━━

   It's not necessary to initialize f to undefined.
  
  > 1 │ let f = /**/undefined/**/ ;
         ^^^^^^^^^^^^^^^
    2 │ 
  
   A variable that is declared and not initialized to any value automatically gets the value of undefined.
  
   Unsafe fix: Remove undefined initialization.
  
    1 │ let·f·=·/**/undefined/**/·;
        -------------------- 
var a = 1;
class Foo {
bar = undefined;
}