TruthFocus News.

Delivering accurate, balanced news and essential information for an informed public.

public policy

eslint error Unary operator '++' used no-plusplus

By Ethan Hayes

I am getting error with my for loop if I used i++ in for loop

var foo = 0; foo++; var bar = 42; bar--; for (i = 0; i < 1; i++) { return; } 
4

5 Answers

One option would be to replace i++ with i+=1

You can also turn that specific eslint rule off (either for the specific line, the file or global configuration). Please consider that this might be not recommended, especially at the file or line level.

The rule name you are looking for is no-plusplus.

Disable it globally

In your eslint config file add the following:

'no-plusplus': 'off' **OR** 'no-plusplus': 0 

There is also an option to disable it only for the for loops:

 no-plusplus: ["error", { "allowForLoopAfterthoughts": true }] 

For further information you can check eslint no-plusplus docs

Disable it at the file level

At the top of your file add the following:

/* eslint-disable no-plusplus */ 

Disable it for the given line

Just before the for loop, add the following:

/* eslint-disable-next-line no-plusplus */ 
2

I got solution of this problem

if we are use i++ in our code eslint give error. For avoiding this type of error we have to use

var foo = 0; foo += 1; var bar = 42; bar -= 1; for (i = 0; i < l; i += 1) { return; } 

Thanks

1

eslint no-plusplus: "error"

Will throw the error on following instances

var foo = 0; foo++; var bar = 42; bar--; for (i = 0; i < l; i++) { return; } 

Use as follows to fix the lint issues so it will go by the rules

var foo = 0; foo += 1; var bar = 42; bar -= 1; for (i = 0; i < l; i += 1) { return; } 

Documentation

As you can see this is a linting error. Either write the code like this,

foo += 1;

and

i += 1 

Or turn that eslint rule off. (not a good idea);

As per Eslint doc, using these operators are subject to automatic semicolon insertion and differences in whitespace can change the semantics of source code.

So it is not allowed in eslint rule, but you can ignore it using this line:

/* eslint no-plusplus: "error" */ 

for reference eslint doc:

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobW5uYm2GdYSOnqqloZ6peqa%2B0aipZq2elr%2B6ec6pnKuZpKS%2FbsHSnptmpp9iva3B0qmjrqs%3D