|
| 1 | +--- |
| 2 | +title: no-unassigned-vars |
| 3 | +rule_type: problem |
| 4 | +related_rules: |
| 5 | +- init-declarations |
| 6 | +- no-unused-vars |
| 7 | +- prefer-const |
| 8 | +--- |
| 9 | + |
| 10 | + |
| 11 | +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. |
| 12 | + |
| 13 | +For example, if you check the value of a `status` variable, but it was never given a value, it will always be `undefined`: |
| 14 | + |
| 15 | +```js |
| 16 | +let status; |
| 17 | + |
| 18 | +// ...forgot to assign a value to status... |
| 19 | + |
| 20 | +if (status === 'ready') { |
| 21 | + console.log('Ready!'); |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +## Rule Details |
| 26 | + |
| 27 | +Examples of **incorrect** code for this rule: |
| 28 | + |
| 29 | +::: incorrect |
| 30 | + |
| 31 | +```js |
| 32 | +/*eslint no-unassigned-vars: "error"*/ |
| 33 | + |
| 34 | +let status; |
| 35 | +if (status === 'ready') { |
| 36 | + console.log('Ready!'); |
| 37 | +} |
| 38 | + |
| 39 | +let user; |
| 40 | +greet(user); |
| 41 | + |
| 42 | +function test() { |
| 43 | + let error; |
| 44 | + return error || "Unknown error"; |
| 45 | +} |
| 46 | + |
| 47 | +let options; |
| 48 | +const { debug } = options || {}; |
| 49 | + |
| 50 | +let flag; |
| 51 | +while (!flag) { |
| 52 | + // Do something... |
| 53 | +} |
| 54 | + |
| 55 | +let config; |
| 56 | +function init() { |
| 57 | + return config?.enabled; |
| 58 | +} |
| 59 | +``` |
| 60 | +
|
| 61 | +::: |
| 62 | +
|
| 63 | +In TypeScript: |
| 64 | +
|
| 65 | +::: incorrect |
| 66 | +
|
| 67 | +```ts |
| 68 | +/*eslint no-unassigned-vars: "error"*/ |
| 69 | + |
| 70 | +let value: number | undefined; |
| 71 | +console.log(value); |
| 72 | +``` |
| 73 | +
|
| 74 | +::: |
| 75 | +
|
| 76 | +Examples of **correct** code for this rule: |
| 77 | +
|
| 78 | +::: correct |
| 79 | +
|
| 80 | +```js |
| 81 | +/*eslint no-unassigned-vars: "error"*/ |
| 82 | + |
| 83 | +let message = "hello"; |
| 84 | +console.log(message); |
| 85 | + |
| 86 | +let user; |
| 87 | +user = getUser(); |
| 88 | +console.log(user.name); |
| 89 | + |
| 90 | +let count; |
| 91 | +count = 1; |
| 92 | +count++; |
| 93 | + |
| 94 | +// Variable is unused (should be reported by `no-unused-vars` only) |
| 95 | +let temp; |
| 96 | + |
| 97 | +let error; |
| 98 | +if (somethingWentWrong) { |
| 99 | + error = "Something went wrong"; |
| 100 | +} |
| 101 | +console.log(error); |
| 102 | + |
| 103 | +let item; |
| 104 | +for (item of items) { |
| 105 | + process(item); |
| 106 | +} |
| 107 | + |
| 108 | +let config; |
| 109 | +function setup() { |
| 110 | + config = { debug: true }; |
| 111 | +} |
| 112 | +setup(); |
| 113 | +console.log(config); |
| 114 | + |
| 115 | +let one = undefined; |
| 116 | +if (one === two) { |
| 117 | + // Noop |
| 118 | +} |
| 119 | +``` |
| 120 | +
|
| 121 | +::: |
| 122 | +
|
| 123 | +In TypeScript: |
| 124 | +
|
| 125 | +::: correct |
| 126 | +
|
| 127 | +```ts |
| 128 | +/*eslint no-unassigned-vars: "error"*/ |
| 129 | + |
| 130 | +declare let value: number | undefined; |
| 131 | +console.log(value); |
| 132 | +``` |
| 133 | +
|
| 134 | +::: |
| 135 | +
|
| 136 | +## When Not To Use It |
| 137 | +
|
| 138 | +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: |
| 139 | +
|
| 140 | +- Legacy codebases where uninitialized variables are used as placeholders. |
| 141 | +- Certain TypeScript use cases where variables are declared with a type and intentionally left unassigned (though using `declare` is preferred). |
0 commit comments