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

Use ternary statements judiciously - JavaScript Tutorial

From the course: JavaScript: Best Practices for Data

Start my 1-month free trial

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…

Contents