Last updated: Mar 7, 2024
Reading time·3 min
no-plusplus
rule globallyno-plusplus
rule for a single lineno-plusplus
rule for an entire fileThe ESLint error "Unary operator '++' used no-plusplus" is raised when you use
the unary ++
or --
operators in your code.
To solve the error, use a reassignment instead, e.g. i += 1
.
Here are 3 examples of how the error occurs.
let i = 0; // ⛔️ Unary operator '++' used.eslint no-plusplus i++; // ⛔️ Unary operator '--' used.eslint no-plusplus i--; // ⛔️ Unary operator '++' used. eslint no-plusplus for ( let index = 0; index < ['bobby', 'hadz', 'com'].length; index++ ) { console.log(index); }
ESLint forbids the use of the unary ++
and --
operators because they are
subject to automatic semicolon insertion and differences in whitespace could
change the semantics of the code.
Instead, you should use += 1
or -= 1
to reassign the variable.
let i = 0; i += 1; console.log(i); // 1 i -= 1; console.log(i); // 0 for ( let index = 0; index < ['bobby', 'hadz', 'com'].length; index += 1 ) { console.log(index); // 0, 1, 2 }
The +=
operator reassigns the variable to its current value plus 1.
Conversely, the -=
operator reassigns the variable to its current value
minus 1.
Here is an example of why using the unary ++
and --
operators could cause
issues.
Everything in the following code sample works as expected.
let a = 1; let b = 2; a++ b // a = 2, b = 2
However, in the following code sample, the b
variable gets incremented, not
a
.
let a = 1; let b = 2; a ++ b // a = 1, b = 3
no-plusplus
rule globallyIf you want to disable the no-plusplus
rule globally, you have to edit your
.eslintrc.js
file.
module.exports = { rules: { 'no-plusplus': 'off', }, };
You could also only disable the rule in for
loops.
module.exports = { rules: { 'no-plusplus': ['error', {allowForLoopAfterthoughts: true}], }, };
If you use a .eslintrc
or .eslintrc.json
file, make sure to double-quote the
string properties and values.
{ "rules": { "no-plusplus": "off" } }
Note that there shouldn't be a trailing comma after the last key-value pair when using JSON.
If you only want to disable the rule in for
loops in a JSON config file, use
the following line instead.
{ "rules": { "no-plusplus": ["error", { "allowForLoopAfterthoughts": true }] } }
no-plusplus
rule for a single lineIf you want to disable the no-plusplus
rule for a single line, use the
following comment.
let i = 0; // eslint-disable-next-line no-plusplus i++;
Note that the comment has to be added above the line on which the error occurs.
The comment disables the rule for a single line.
no-plusplus
rule for an entire fileIf you want to disable the rule for an entire file, add the following comment to the top of your file.
/* eslint-disable no-plusplus */ let i = 0; i++; i--; for (let index = 0; index < ['bobby', 'hadz', 'com'].length; index++) { console.log(index); }
Make sure to add the comment at the top of your file or at least above any code
that uses the unary ++
and --
operators.
The same approach can be used to disable the no-plusplus
rule for a block of
code.
/* eslint-disable no-plusplus */ let i = 0; i++; i--; /* eslint-enable no-plusplus */ // ⛔️ Unary operator '++' used. eslint no-plusplus for (let index = 0; index < ['bobby', 'hadz', 'com'].length; index++) { console.log(index); }
The first comment in the example disables the rule and the second comment enables it.
Using the unary ++
operator in the for
loop raises the error because the
for
loop comes after the ESLint comment that enables the no-plusplus
rule.
You can learn more about the related topics by checking out the following tutorials: