mirror of
https://github.com/airbnb/javascript.git
synced 2026-01-14 11:18:04 -05:00
add section 15.5 - Ternaries
add link to the relevant eslint rule
This commit is contained in:
23
README.md
23
README.md
@@ -1246,6 +1246,29 @@ Other Style Guides
|
||||
|
||||
- [15.4](#15.4) <a name='15.4'></a> For more information see [Truth Equality and JavaScript](http://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/#more-2108) by Angus Croll.
|
||||
|
||||
- [15.5](#15.5) <a name='15.5'></a> Ternaries should not be nested and generally be single line expressions.
|
||||
|
||||
eslint rules: [`no-nested-ternary`](http://eslint.org/docs/rules/no-nested-ternary.html).
|
||||
|
||||
```javascript
|
||||
//bad
|
||||
const foo = maybe1 > maybe2
|
||||
? "bar"
|
||||
: value1 > value2 ? "baz" : null;
|
||||
|
||||
//better
|
||||
const maybeNull = value1 > value2 ? 'baz' : null;
|
||||
|
||||
const foo = maybe1 > maybe2
|
||||
? 'bar'
|
||||
: maybeNull;
|
||||
|
||||
//best
|
||||
const maybeNull = value1 > value2 ? 'baz' : null;
|
||||
|
||||
const foo = maybe1 > maybe2 ? 'bar' : maybeNull;
|
||||
```
|
||||
|
||||
**[⬆ back to top](#table-of-contents)**
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user