Skip to content

Commit 6945dd9

Browse files
committed
add section 15.5 - Ternaries
1 parent af81beb commit 6945dd9

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,27 @@ Other Style Guides
12891289
12901290
- [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.
12911291
1292+
- [15.5](#15.5) <a name='15.5'></a> Ternaries should not be nested and generally be single line expressions.
1293+
1294+
```javascript
1295+
//bad
1296+
const foo = maybe1 > maybe2
1297+
? "bar"
1298+
: value1 > value2 ? "baz" : null;
1299+
1300+
//better
1301+
const maybeNull = value1 > value2 ? 'baz' : null;
1302+
1303+
const foo = maybe1 > maybe2
1304+
? 'bar'
1305+
: maybeNull;
1306+
1307+
//best
1308+
const maybeNull = value1 > value2 ? 'baz' : null;
1309+
1310+
const foo = maybe1 > maybe2 ? 'bar' : maybeNull;
1311+
```
1312+
12921313
**[⬆ back to top](#table-of-contents)**
12931314
12941315

0 commit comments

Comments
 (0)