]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/no-ternary.md
3c95f827d1ebe06b6160324c06c5f9f1d06b4794
[pve-eslint.git] / eslint / docs / rules / no-ternary.md
1 # disallow ternary operators (no-ternary)
2
3 The ternary operator is used to conditionally assign a value to a variable. Some believe that the use of ternary operators leads to unclear code.
4
5 ```js
6 var foo = isBar ? baz : qux;
7 ```
8
9 ## Rule Details
10
11 This rule disallows ternary operators.
12
13 Examples of **incorrect** code for this rule:
14
15 ```js
16 /*eslint no-ternary: "error"*/
17
18 var foo = isBar ? baz : qux;
19
20 function quux() {
21 return foo ? bar() : baz();
22 }
23 ```
24
25 Examples of **correct** code for this rule:
26
27 ```js
28 /*eslint no-ternary: "error"*/
29
30 var foo;
31
32 if (isBar) {
33 foo = baz;
34 } else {
35 foo = qux;
36 }
37
38 function quux() {
39 if (foo) {
40 return bar();
41 } else {
42 return baz();
43 }
44 }
45 ```
46
47 ## Related Rules
48
49 * [no-nested-ternary](no-nested-ternary.md)
50 * [no-unneeded-ternary](no-unneeded-ternary.md)