question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mixed operators with same precedence

See original GitHub issue

The 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:closed
  • Created 7 years ago
  • Reactions:12
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

15reactions
aboytoncommented, Sep 13, 2016

Thanks for the quick reply.

Actually || and && do have different precedence (see MDN operator precedence or Microsoft operator precedence). As an example true || true && false is true as && has a higher precedence than ||.

Because of this (with "allowSamePrecedence": true) the following are lint errors:

var foo = a || b && c;
var foo = a + b * c;

but these are fine

var foo = a || (b && c);
var foo = a + (b * c);
var foo = a + b - c;

I personally don’t mind which way you go for a + b * c or a + (b * c) (I never get confused here but I get how some people do) but it would be nice to make a + b - c valid code.

3reactions
ljharbcommented, Jan 11, 2018

The next (semver-major) release will include it, when we’re ready to release it.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found