1 # Enforce or disallow newlines between operands of ternary expressions (multiline-ternary)
3 JavaScript allows operands of ternary expressions to be separated by newlines, which can improve the readability of your program.
8 var foo = bar > baz ? value1 : value2;
11 The above can be rewritten as the following to improve readability and more clearly delineate the operands:
21 This rule enforces or disallows newlines between operands of a ternary expression.
22 Note: The location of the operators is not enforced by this rule. Please see the [operator-linebreak](operator-linebreak.md) rule if you are interested in enforcing the location of the operators themselves.
26 This rule has a string option:
28 * `"always"` (default) enforces newlines between the operands of a ternary expression.
29 * `"always-multiline"` enforces newlines between the operands of a ternary expression if the expression spans multiple lines.
30 * `"never"` disallows newlines between the operands of a ternary expression.
34 This is the default option.
36 Examples of **incorrect** code for this rule with the `"always"` option:
39 /*eslint multiline-ternary: ["error", "always"]*/
41 foo > bar ? value1 : value2;
50 Examples of **correct** code for this rule with the `"always"` option:
53 /*eslint multiline-ternary: ["error", "always"]*/
68 Examples of **incorrect** code for this rule with the `"always-multiline"` option:
71 /*eslint multiline-ternary: ["error", "always-multiline"]*/
80 bar > baz ? value1 : value2;
83 Examples of **correct** code for this rule with the `"always-multiline"` option:
86 /*eslint multiline-ternary: ["error", "always-multiline"]*/
88 foo > bar ? value1 : value2;
95 (baz > qux ? value1 : value2) :
112 Examples of **incorrect** code for this rule with the `"never"` option:
115 /*eslint multiline-ternary: ["error", "never"]*/
129 Examples of **correct** code for this rule with the `"never"` option:
132 /*eslint multiline-ternary: ["error", "never"]*/
134 foo > bar ? value1 : value2;
136 foo > bar ? (baz > qux ? value1 : value2) : value3;
139 baz > qux ? value1 : value2
143 ## When Not To Use It
145 You can safely disable this rule if you do not have any strict conventions about whether the operands of a ternary expression should be separated by newlines.
149 * [operator-linebreak](operator-linebreak.md)
153 * **JSCS**: [requireMultiLineTernary](https://jscs-dev.github.io/rule/requireMultiLineTernary)