Versions

no-unassigned-vars

Disallow let or var variables that are read but never assigned

This rule flags let or var declarations that are never assigned a value but are still read or used in the code. Since these variables will always be undefined, their usage is likely a programming mistake.

For example, if you check the value of a status variable, but it was never given a value, it will always be undefined:

let status;

// ...forgot to assign a value to status...

if (status === 'ready') {
  console.log('Ready!');
}

Rule Details

Examples of incorrect code for this rule:

Open in Playground
/*eslint no-unassigned-vars: "error"*/

let status;
if (status === 'ready') {
  console.log('Ready!');
}

let user;
greet(user);

function test() {
  let error;
  return error || "Unknown error";
}

let options;
const { debug } = options || {};

let flag;
while (!flag) {
  // Do something...
}

let config;
function init() {
  return config?.enabled;
}

In TypeScript:

/*eslint no-unassigned-vars: "error"*/

let value: number | undefined;
console.log(value);

Examples of correct code for this rule:

Open in Playground
/*eslint no-unassigned-vars: "error"*/

let message = "hello";
console.log(message);

let user;
user = getUser();
console.log(user.name);

let count;
count = 1;
count++;

// Variable is unused (should be reported by `no-unused-vars` only)
let temp;

let error;
if (somethingWentWrong) {
  error = "Something went wrong";
}
console.log(error);

let item;
for (item of items) {
  process(item);
}

let config;
function setup() {
  config = { debug: true };
}
setup();
console.log(config);

let one = undefined;
if (one === two) {
  // Noop
}

In TypeScript:

/*eslint no-unassigned-vars: "error"*/

declare let value: number | undefined;
console.log(value);

When Not To Use It

You can disable this rule if your code intentionally uses variables that are declared and used, but are never assigned a value. This might be the case in:

  • Legacy codebases where uninitialized variables are used as placeholders.
  • Certain TypeScript use cases where variables are declared with a type and intentionally left unassigned (though using declare is preferred).

Resources

Change Language