Skip to main content

prefer-plusplus

Require ++ or -- operators instead of assignment operators where applicable.

Rule Details

The operator-assignment ESLint rule converts x = x + 1 to x += 1. This is a fantastic rule because it makes code more concise and easier to read. (Technically, the code would be more confusing for people who don't know what the "+=" operator does, but this is not an issue in most cases.)

Building on this logic, it also makes sense to convert x += 1 to x++, which is even more concise and easier to read. (Again, we make the assumption that everyone knows what the "++" operator does, which should be a pretty safe bet.)

However, the ++ operator is historically controversial in JavaScript. For example, the Airbnb style guide gives this justification:

Why? Per the eslint documentation, unary increment and decrement statements are subject to automatic semicolon insertion and can cause silent errors with incrementing or decrementing values within an application. It is also more expressive to mutate your values with statements like num += 1 instead of num++ or num ++. Disallowing unary increment and decrement statements also prevents you from pre-incrementing/pre-decrementing values unintentionally which can also cause unexpected behavior in your programs.

This justification does not apply if you use the combination of:

Together, these heavily restrict the usage of the operator, making the only legal usage equal to that of "+= 1".

// Bad
i += 1;

// Good
i++;

Options and Defaults

{
"rules": {
"complete/prefer-plusplus": "error"
}
}

This rule is not configurable.

Resources