From the course: JavaScript: Best Practices for Data
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Use ternary statements judiciously - JavaScript Tutorial
From the course: JavaScript: Best Practices for Data
Use ternary statements judiciously
- My code for this video using a couple ternary statements to choose between values based on the truth value of a condition. On line seven, I'm setting a Boolean value for a variable based on the value of a property of my object. On line nine, I'm choosing the text to be logged based on whether I have custom text to display. Turnery statements are meant to serve as a quick substitute for larger logic patterns when you only need to make a quick comparison. They're more compact, which means you have the classic trade off of fewer characters at the cost of denser code that can be harder for a human to parse. Because of the potential draw backs of ternary statements, it's important to use them only when necessary. ESLint has a rule that flags ternaries that could easily be replaced. It also has a rule that lets you flag nested ternaries for elimination. Nested ternaries make that short, hard-to-parse code even more complex and to…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
(Locked)
Use triple-character equality operators2m 56s
-
(Locked)
Don't use Yoda conditions1m 52s
-
(Locked)
Compare appropriately for the data type2m 2s
-
(Locked)
Use ternary statements judiciously3m 23s
-
(Locked)
Add parentheses to clarify logic2m 3s
-
(Locked)
Challenge: Compare values53s
-
(Locked)
Solution: Compare values9m 29s
-
(Locked)
-