]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/no-cond-assign.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / no-cond-assign.md
CommitLineData
8f9d1d4d
DC
1---
2title: no-cond-assign
8f9d1d4d
DC
3rule_type: problem
4related_rules:
5- no-extra-parens
6---
7
8
eb39fafa
DC
9
10In conditional statements, it is very easy to mistype a comparison operator (such as `==`) as an assignment operator (such as `=`). For example:
11
12```js
13// Check the user's job title
14if (user.jobTitle = "manager") {
15 // user.jobTitle is now incorrect
16}
17```
18
19There are valid reasons to use assignment operators in conditional statements. However, it can be difficult to tell whether a specific assignment was intentional.
20
21## Rule Details
22
23This rule disallows ambiguous assignment operators in test conditions of `if`, `for`, `while`, and `do...while` statements.
24
25## Options
26
27This rule has a string option:
28
29* `"except-parens"` (default) allows assignments in test conditions *only if* they are enclosed in parentheses (for example, to allow reassigning a variable in the test of a `while` or `do...while` loop)
30* `"always"` disallows all assignments in test conditions
31
32### except-parens
33
34Examples of **incorrect** code for this rule with the default `"except-parens"` option:
35
8f9d1d4d
DC
36::: incorrect
37
eb39fafa
DC
38```js
39/*eslint no-cond-assign: "error"*/
40
41// Unintentional assignment
42var x;
43if (x = 0) {
44 var b = 1;
45}
46
47// Practical example that is similar to an error
48function setHeight(someNode) {
49 "use strict";
50 do {
51 someNode.height = "100px";
52 } while (someNode = someNode.parentNode);
53}
54```
55
8f9d1d4d
DC
56:::
57
eb39fafa
DC
58Examples of **correct** code for this rule with the default `"except-parens"` option:
59
8f9d1d4d
DC
60::: correct
61
eb39fafa
DC
62```js
63/*eslint no-cond-assign: "error"*/
64
65// Assignment replaced by comparison
66var x;
67if (x === 0) {
68 var b = 1;
69}
70
71// Practical example that wraps the assignment in parentheses
72function setHeight(someNode) {
73 "use strict";
74 do {
75 someNode.height = "100px";
76 } while ((someNode = someNode.parentNode));
77}
78
79// Practical example that wraps the assignment and tests for 'null'
80function setHeight(someNode) {
81 "use strict";
82 do {
83 someNode.height = "100px";
84 } while ((someNode = someNode.parentNode) !== null);
85}
86```
87
8f9d1d4d
DC
88:::
89
eb39fafa
DC
90### always
91
92Examples of **incorrect** code for this rule with the `"always"` option:
93
8f9d1d4d
DC
94::: incorrect
95
eb39fafa
DC
96```js
97/*eslint no-cond-assign: ["error", "always"]*/
98
99// Unintentional assignment
100var x;
101if (x = 0) {
102 var b = 1;
103}
104
105// Practical example that is similar to an error
106function setHeight(someNode) {
107 "use strict";
108 do {
109 someNode.height = "100px";
110 } while (someNode = someNode.parentNode);
111}
112
113// Practical example that wraps the assignment in parentheses
114function setHeight(someNode) {
115 "use strict";
116 do {
117 someNode.height = "100px";
118 } while ((someNode = someNode.parentNode));
119}
120
121// Practical example that wraps the assignment and tests for 'null'
122function setHeight(someNode) {
123 "use strict";
124 do {
125 someNode.height = "100px";
126 } while ((someNode = someNode.parentNode) !== null);
127}
128```
129
8f9d1d4d
DC
130:::
131
eb39fafa
DC
132Examples of **correct** code for this rule with the `"always"` option:
133
8f9d1d4d
DC
134::: correct
135
eb39fafa
DC
136```js
137/*eslint no-cond-assign: ["error", "always"]*/
138
139// Assignment replaced by comparison
140var x;
141if (x === 0) {
142 var b = 1;
143}
144```
145
8f9d1d4d 146:::