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.

Add parentheses to clarify logic

Add parentheses to clarify logic - JavaScript Tutorial

From the course: JavaScript: Best Practices for Data

Start my 1-month free trial

Add parentheses to clarify logic

- [Instructor] The start code for this video generates three choices randomly from an array, then it has the logic to ensure all three are different. Line 21 is pretty straightforward, but in line 24, I'm checking to make sure the third choice variable is different from first choice and from second choice. If my screen was a little smaller, that statement would even run over a couple lines, and it uses a total of three operators. That can make it hard to read, and every time I come to this line while debugging, I have to stop for a minute to remember just what it's doing. Both mathematical and logical operations in JavaScript have a specific order that the parser follows. For instance, this is the order of operations for a few common logical operators. As long as I'm writing code with this order in mind, it'll work correctly. But there's a difference between a parser understanding the code and me or another developer…

Contents