Add new formatting example (#20937)

Illustrates common multiple line formatting for chained ternary operators
This commit is contained in:
kViking
2018-11-05 14:46:18 -08:00
committed by Huyen Nguyen
parent ba6458bcce
commit f6e71ede55

View File

@ -57,6 +57,20 @@ You can also chain a ternary operator indefinitely, in a similar way to using `e
}
```
To ease readability, this is often formatted across multiple lines.
```javascript
function displayNum(num) {
return num === 3
? 'number is 3'
: num === 2
? 'number is 2'
: num === 1
? 'number is 1'
: 'number is not in range';
}
```
This method needs to be used sparingly in the right places however, as with multiple `else if's` it can sometimes lead to more readable code using a switch statement.