]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/no-plusplus.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / no-plusplus.md
CommitLineData
8f9d1d4d
DC
1---
2title: no-plusplus
8f9d1d4d
DC
3rule_type: suggestion
4---
5
eb39fafa
DC
6
7Because the unary `++` and `--` operators are subject to automatic semicolon insertion, differences in whitespace can change semantics of source code.
8
9```js
10var i = 10;
11var j = 20;
12
13i ++
14j
15// i = 11, j = 20
16```
17
18```js
19var i = 10;
20var j = 20;
21
22i
23++
24j
25// i = 10, j = 21
26```
27
28## Rule Details
29
30This rule disallows the unary operators `++` and `--`.
31
32Examples of **incorrect** code for this rule:
33
8f9d1d4d
DC
34::: incorrect
35
eb39fafa
DC
36```js
37/*eslint no-plusplus: "error"*/
38
39var foo = 0;
40foo++;
41
42var bar = 42;
43bar--;
44
45for (i = 0; i < l; i++) {
46 return;
47}
48```
49
8f9d1d4d
DC
50:::
51
eb39fafa
DC
52Examples of **correct** code for this rule:
53
8f9d1d4d
DC
54::: correct
55
eb39fafa
DC
56```js
57/*eslint no-plusplus: "error"*/
58
59var foo = 0;
60foo += 1;
61
62var bar = 42;
63bar -= 1;
64
65for (i = 0; i < l; i += 1) {
66 return;
67}
68```
69
8f9d1d4d
DC
70:::
71
eb39fafa
DC
72## Options
73
74This rule has an object option.
75
76* `"allowForLoopAfterthoughts": true` allows unary operators `++` and `--` in the afterthought (final expression) of a `for` loop.
77
78### allowForLoopAfterthoughts
79
80Examples of **correct** code for this rule with the `{ "allowForLoopAfterthoughts": true }` option:
81
8f9d1d4d
DC
82::: correct
83
eb39fafa
DC
84```js
85/*eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }]*/
86
87for (i = 0; i < l; i++) {
88 doSomething(i);
89}
90
91for (i = l; i >= 0; i--) {
92 doSomething(i);
93}
94
95for (i = 0, j = l; i < l; i++, j--) {
96 doSomething(i, j);
97}
98```
99
8f9d1d4d
DC
100:::
101
eb39fafa
DC
102Examples of **incorrect** code for this rule with the `{ "allowForLoopAfterthoughts": true }` option:
103
8f9d1d4d
DC
104::: incorrect
105
eb39fafa
DC
106```js
107/*eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }]*/
108
109for (i = 0; i < l; j = i++) {
110 doSomething(i, j);
111}
112
113for (i = l; i--;) {
114 doSomething(i);
115}
116
117for (i = 0; i < l;) i++;
118```
8f9d1d4d
DC
119
120:::