]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/no-multi-assign.md
import eslint 7.28.0
[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;
29```
30
31Examples of **correct** code for this rule:
32
33```js
34/*eslint no-multi-assign: "error"*/
35var a = 5;
36var b = 5;
37var c = 5;
38
39const foo = "baz";
40const bar = "baz";
41
42let a = c;
43let b = c;
44```
45
5422a9cc
TL
46## Options
47
48This rule has an object option:
49
50* `"ignoreNonDeclaration"`: When set to `true`, the rule allows chains that don't include initializing a variable in a declaration. Default is `false`.
51
52### ignoreNonDeclaration
53
54Examples of **correct** code for the `{ "ignoreNonDeclaration": true }` option:
55
56```js
57/*eslint no-multi-assign: ["error", { "ignoreNonDeclaration": true }]*/
58
59let a;
60let b;
61a = b = "baz";
62
63const x = {};
64const y = {};
65x.one = y.one = 1;
66```
67
68Examples of **incorrect** code for the `{ "ignoreNonDeclaration": true }` option:
69
70```js
71/*eslint no-multi-assign: ["error", { "ignoreNonDeclaration": true }]*/
72
73let a = b = "baz";
74
75const foo = bar = 1;
76```
77
eb39fafa
DC
78## Related Rules
79
80* [max-statements-per-line](max-statements-per-line.md)