2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-07-14 14:06:17 +02:00
Operator precedence determines the order in which different operators are evaluated when an expression contains multiple ones. It helps determine how the expression is parsed and executed. JavaScript follows a specific set of rules to determine operator precedence.
2021-04-28 16:49:39 +02:00
2023-07-14 14:06:17 +02:00
Not being aware of JavaScript's operator precedence rules can lead to unexpected and potentially incorrect results when evaluating expressions. This is common when misapplying the logical negation operator (``++!++``). For instance, consider the difference between ``++!key in dict++`` and ``++!(key in dict)++``. The first looks for a boolean value (``++!key++``) in ``++dict++``, and the other looks for a string and inverts the result. The same applies for ``++!obj instanceof SomeClass++`
2021-04-28 16:49:39 +02:00
2023-07-14 14:06:17 +02:00
This rule raises an issue when the left operand of an ``++in++`` or ``++instanceof++`` operator is negated with ``++!++``.
2021-04-28 16:49:39 +02:00
2023-07-14 14:06:17 +02:00
[source,javascript,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
2023-07-14 14:06:17 +02:00
if (!"prop" in myObj) { // Noncompliant: checks whether !"prop", that is, false is in myObj
doTheThing(); // this block is never executed
2021-04-28 16:49:39 +02:00
}
2023-07-14 14:06:17 +02:00
if (!foo instanceof MyClass) { // Noncompliant: "!foo" returns a boolean, which is not an instance of anything
doTheOtherThing(); // this block is never executed either
2021-04-28 16:49:39 +02:00
}
----
2023-07-14 14:06:17 +02:00
You should use parentheses to force the order of evaluation of expressions mixing negation and ``++in++`` or ``++instanceof++`` operators.
2021-04-28 18:08:03 +02:00
2023-07-14 14:06:17 +02:00
[source,javascript,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
if (!("prop" in myObj)) {
doTheThing();
}
if (!(foo instanceof MyClass)) {
doTheOtherThing();
}
----
2021-04-28 18:08:03 +02:00
2023-07-14 14:06:17 +02:00
== Resources
=== Documentation
2023-10-19 11:46:59 +02:00
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_precedence[Operator precedence]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_precedence#table[Operator precedence table]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT[Logical NOT (``++!++``)]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof[``++instanceof++``]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in[``++in++`` operator]
2021-04-28 18:08:03 +02:00
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Add parentheses to disambiguate this expression.
=== Highlighting
minus operator
2021-09-20 15:38:42 +02:00
endif::env-github,rspecator-view[]