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