Mixed operators with same precedence
See original GitHub issueThe ESLint rule no-mixed-operators is great for making things like
var foo = a && b < 0 || c > 0 || d + 1 === 0;
var bar = a && b || c;
clearer by adding parentheses (rather than having people remember the precedence of &&
and ||
. That said, I’m unconvinced that
var foo = a + b - c
is clearer with parentheses, that is according to this style guide you’d need to write this as
var foo = (a + b) - c
The no-mixed-operators rule has an option for this, allowSamePrecedence
and the default is true
meaning that var foo = a + b - c
is valid code (which I think makes a lot more sense). Is there any reason for having this set to false
?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:12
- Comments:16 (4 by maintainers)
Top Results From Across the Web
Operator precedence - JavaScript - MDN Web Docs - Mozilla
Operators are first grouped by precedence, and then, for adjacent operators that have the same precedence, by associativity. So, when mixing ...
Read more >no-mixed-operators - ESLint - Pluggable JavaScript Linter
allowSamePrecedence ( boolean ) - specifies whether to allow mixed operators if they are of equal precedence. Default is true .
Read more >Order of precedence of mixed operators in javascript
What would be the order of precedence if different kind of operators are used in same expression without parantheses?
Read more >Precedence of Logical Operators
In an expression, the operator with the highest precedence is grouped with its operand(s) first, then the next highest operator will be grouped...
Read more >Operator Precedence | Mixed Expression in Java
If two operators sharing an operand have the same precedence, they work according to their associativity. Associativity is either left to right or...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the quick reply.
Actually
||
and&&
do have different precedence (see MDN operator precedence or Microsoft operator precedence). As an exampletrue || true && false
istrue
as&&
has a higher precedence than||
.Because of this (with
"allowSamePrecedence": true
) the following are lint errors:but these are fine
I personally don’t mind which way you go for
a + b * c
ora + (b * c)
(I never get confused here but I get how some people do) but it would be nice to makea + b - c
valid code.The next (semver-major) release will include it, when we’re ready to release it.