28 lines
1.1 KiB
Plaintext
28 lines
1.1 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Ternary operator should not be used to select between two boolean values, or instead of a logical `OR` operation. Ternary expressions are often difficult to read, so if a simpler syntax exists, it should be used instead of a ternary expression. This happens when
|
|
|
|
- the expression returns two boolean values
|
|
|
|
[source,javascript]
|
|
----
|
|
let isGood = value > 0 ? true : false; // Non-compliant, replace with value > 0
|
|
let isBad = value > 0 ? false : true; // Non-compliant, replace with !(value > 0)
|
|
----
|
|
|
|
- the same value is used for both the conditional test and the consequent
|
|
|
|
[source,javascript]
|
|
----
|
|
let a = x ? x : y; // Non-compliant, replace with x || y
|
|
----
|
|
|
|
|
|
== Resources
|
|
|
|
=== Documentation
|
|
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_operator[Ternary operator]
|
|
* 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/Logical_OR[Logical OR (||)]
|