]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/init-declarations.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / init-declarations.md
1 # require or disallow initialization in variable declarations (init-declarations)
2
3 In JavaScript, variables can be assigned during declaration, or at any point afterwards using an assignment statement. For example, in the following code, `foo` is initialized during declaration, while `bar` is initialized later.
4
5 ```js
6 var foo = 1;
7 var bar;
8
9 if (foo) {
10 bar = 1;
11 } else {
12 bar = 2;
13 }
14 ```
15
16 ## Rule Details
17
18 This rule is aimed at enforcing or eliminating variable initializations during declaration. For example, in the following code, `foo` is initialized during declaration, while `bar` is not.
19
20 ```js
21 var foo = 1;
22 var bar;
23
24 bar = 2;
25 ```
26
27 This rule aims to bring consistency to variable initializations and declarations.
28
29 ## Options
30
31 The rule takes two options:
32
33 1. A string which must be either `"always"` (the default), to enforce initialization at declaration, or `"never"` to disallow initialization during declaration. This rule applies to `var`, `let`, and `const` variables, however `"never"` is ignored for `const` variables, as unassigned `const`s generate a parse error.
34 2. An object that further controls the behavior of this rule. Currently, the only available parameter is `ignoreForLoopInit`, which indicates if initialization at declaration is allowed in `for` loops when `"never"` is set, since it is a very typical use case.
35
36 You can configure the rule as follows:
37
38 Variables must be initialized at declaration (default)
39
40 ```json
41 {
42 "init-declarations": ["error", "always"],
43 }
44 ```
45
46 Variables must not be initialized at declaration
47
48 ```json
49 {
50 "init-declarations": ["error", "never"]
51 }
52 ```
53
54 Variables must not be initialized at declaration, except in for loops, where it is allowed
55
56 ```json
57 {
58 "init-declarations": ["error", "never", { "ignoreForLoopInit": true }]
59 }
60 ```
61
62 ### always
63
64 Examples of **incorrect** code for the default `"always"` option:
65
66 ```js
67 /*eslint init-declarations: ["error", "always"]*/
68 /*eslint-env es6*/
69
70 function foo() {
71 var bar;
72 let baz;
73 }
74 ```
75
76 Examples of **correct** code for the default `"always"` option:
77
78 ```js
79 /*eslint init-declarations: ["error", "always"]*/
80 /*eslint-env es6*/
81
82 function foo() {
83 var bar = 1;
84 let baz = 2;
85 const qux = 3;
86 }
87 ```
88
89 ### never
90
91 Examples of **incorrect** code for the `"never"` option:
92
93 ```js
94 /*eslint init-declarations: ["error", "never"]*/
95 /*eslint-env es6*/
96
97 function foo() {
98 var bar = 1;
99 let baz = 2;
100
101 for (var i = 0; i < 1; i++) {}
102 }
103 ```
104
105 Examples of **correct** code for the `"never"` option:
106
107 ```js
108 /*eslint init-declarations: ["error", "never"]*/
109 /*eslint-env es6*/
110
111 function foo() {
112 var bar;
113 let baz;
114 const buzz = 1;
115 }
116 ```
117
118 The `"never"` option ignores `const` variable initializations.
119
120 ### ignoreForLoopInit
121
122 Examples of **correct** code for the `"never", { "ignoreForLoopInit": true }` options:
123
124 ```js
125 /*eslint init-declarations: ["error", "never", { "ignoreForLoopInit": true }]*/
126 for (var i = 0; i < 1; i++) {}
127 ```
128
129 ## When Not To Use It
130
131 When you are indifferent as to how your variables are initialized.