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.

Don't use Yoda conditions

Don't use Yoda conditions - JavaScript Tutorial

From the course: JavaScript: Best Practices for Data

Start my 1-month free trial

Don't use Yoda conditions

- [Instructor] Check for alert levels, my code does. My code checks for alert levels, and it uses a conditional structure to map numeric values to descriptive text. In each of those comparisons, after each if operator, I've written the literal numeric value first and the variable name I'm comparing to second. If I run my code, it works. But if the code looks funny to you, you're not alone. It's pretty standard in JavaScript to write the variable first, and then the literal value second in comparisons. Flipping them around like I've done here is sometimes called a Yoda condition, in reference to the Star Wars character Yoda, who often reverses the order of his sentences. Just like Yoda's dialogue, all the information is here, and the comparison can be parsed. But for a human, it can take a moment to piece it together and recognize the comparison, because the opposite pattern is so common. I prefer not to use Yoda conditions,…

Contents