ESLint error Unary operator '++' used no-plusplus [Solved]

avatar
Borislav Hadzhiev

Last updated: Mar 7, 2024
3 min

banner

# Table of Contents

  1. ESLint error Unary operator '++' used no-plusplus
  2. Disabling the ESLint no-plusplus rule globally
  3. Disabling the ESLint no-plusplus rule for a single line
  4. Disabling the ESLint no-plusplus rule for an entire file

# ESLint error Unary operator '++' used no-plusplus [Solved]

The 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.

unary operator used eslint no plusplus

Here are 3 examples of how the error occurs.

index.js
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.

index.js
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.

index.js
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.

index.js
let a = 1; let b = 2; a ++ b // a = 1, b = 3

# Disabling the ESLint no-plusplus rule globally

If you want to disable the no-plusplus rule globally, you have to edit your .eslintrc.js file.

.eslintrc.js
module.exports = { rules: { 'no-plusplus': 'off', }, };

disable no plusplus rule globally

You could also only disable the rule in for loops.

.eslintrc.js
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.

.eslintrc.json
{ "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.

.eslintrc.json
{ "rules": { "no-plusplus": ["error", { "allowForLoopAfterthoughts": true }] } }

# Disabling the ESLint no-plusplus rule for a single line

If you want to disable the no-plusplus rule for a single line, use the following comment.

index.js
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.

# Disabling the ESLint no-plusplus rule for an entire file

If you want to disable the rule for an entire file, add the following comment to the top of your file.

index.js
/* 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.

index.js
/* 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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.