]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-ternary.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / no-ternary.md
1 ---
2 title: no-ternary
3 layout: doc
4 rule_type: suggestion
5 related_rules:
6 - no-nested-ternary
7 - no-unneeded-ternary
8 ---
9
10
11 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.
12
13 ```js
14 var foo = isBar ? baz : qux;
15 ```
16
17 ## Rule Details
18
19 This rule disallows ternary operators.
20
21 Examples of **incorrect** code for this rule:
22
23 ::: incorrect
24
25 ```js
26 /*eslint no-ternary: "error"*/
27
28 var foo = isBar ? baz : qux;
29
30 function quux() {
31 return foo ? bar() : baz();
32 }
33 ```
34
35 :::
36
37 Examples of **correct** code for this rule:
38
39 ::: correct
40
41 ```js
42 /*eslint no-ternary: "error"*/
43
44 var foo;
45
46 if (isBar) {
47 foo = baz;
48 } else {
49 foo = qux;
50 }
51
52 function quux() {
53 if (foo) {
54 return bar();
55 } else {
56 return baz();
57 }
58 }
59 ```
60
61 :::