]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/no-multi-assign.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / no-multi-assign.md
CommitLineData
eb39fafa
DC
1# Disallow Use of Chained Assignment Expressions (no-multi-assign)
2
3Chaining the assignment of variables can lead to unexpected results and be difficult to read.
4
5```js
6(function() {
7 const foo = bar = 0; // Did you mean `foo = bar == 0`?
8 bar = 1; // This will not fail since `bar` is not constant.
9})();
10console.log(bar); // This will output 1 since `bar` is not scoped.
11```
12
13## Rule Details
14
15This rule disallows using multiple assignments within a single statement.
16
17Examples of **incorrect** code for this rule:
18
19```js
20/*eslint no-multi-assign: "error"*/
21
22var a = b = c = 5;
23
24const foo = bar = "baz";
25
26let a =
27 b =
28 c;
609c276f
TL
29
30class Foo {
31 a = b = 10;
32}
33
34a = b = "quux";
eb39fafa
DC
35```
36
37Examples of **correct** code for this rule:
38
39```js
40/*eslint no-multi-assign: "error"*/
609c276f 41
eb39fafa
DC
42var a = 5;
43var b = 5;
44var c = 5;
45
46const foo = "baz";
47const bar = "baz";
48
49let a = c;
50let b = c;
609c276f
TL
51
52class Foo {
53 a = 10;
54 b = 10;
55}
56
57a = "quux";
58b = "quux";
eb39fafa
DC
59```
60
5422a9cc
TL
61## Options
62
63This rule has an object option:
64
609c276f 65* `"ignoreNonDeclaration"`: When set to `true`, the rule allows chains that don't include initializing a variable in a declaration or initializing a class field. Default is `false`.
5422a9cc
TL
66
67### ignoreNonDeclaration
68
69Examples of **correct** code for the `{ "ignoreNonDeclaration": true }` option:
70
71```js
72/*eslint no-multi-assign: ["error", { "ignoreNonDeclaration": true }]*/
73
74let a;
75let b;
76a = b = "baz";
77
78const x = {};
79const y = {};
80x.one = y.one = 1;
81```
82
83Examples of **incorrect** code for the `{ "ignoreNonDeclaration": true }` option:
84
85```js
86/*eslint no-multi-assign: ["error", { "ignoreNonDeclaration": true }]*/
87
88let a = b = "baz";
89
90const foo = bar = 1;
609c276f
TL
91
92class Foo {
93 a = b = 10;
94}
5422a9cc
TL
95```
96
eb39fafa
DC
97## Related Rules
98
99* [max-statements-per-line](max-statements-per-line.md)