Skip to content

Commit d293e74

Browse files
committed
add section 15.5 - Ternaries
add link to the relevant eslint rule
1 parent 7eb7b78 commit d293e74

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,29 @@ Other Style Guides
12461246
12471247
- [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.
12481248
1249+
- [15.5](#15.5) <a name='15.5'></a> Ternaries should not be nested and generally be single line expressions.
1250+
1251+
eslint rules: [`no-nested-ternary`](http://eslint.org/docs/rules/no-nested-ternary.html).
1252+
1253+
```javascript
1254+
//bad
1255+
const foo = maybe1 > maybe2
1256+
? "bar"
1257+
: value1 > value2 ? "baz" : null;
1258+
1259+
//better
1260+
const maybeNull = value1 > value2 ? 'baz' : null;
1261+
1262+
const foo = maybe1 > maybe2
1263+
? 'bar'
1264+
: maybeNull;
1265+
1266+
//best
1267+
const maybeNull = value1 > value2 ? 'baz' : null;
1268+
1269+
const foo = maybe1 > maybe2 ? 'bar' : maybeNull;
1270+
```
1271+
12491272
**[⬆ back to top](#table-of-contents)**
12501273
12511274

0 commit comments

Comments
 (0)