Parentheses in Conditions

By: Dusty Arlia
Published on October 30, 2012
Last Updated on Tuesday, July 14, 2015 at 12:10 PM
Total Updates: 4

Whenever you have multiple conditions in a complex condition, you can make your complex condition easier to read with parentheses. Here is an example without parentheses:

if (customer == 'senior' || customer == 'veteran' || customer == 'Chuck Norris') {
alert('You get 50% off!');
}

And here is the same example with parentheses:

if ((customer == 'senior') || (customer == 'veteran') || (customer == 'Chuck Norris')) {
alert('You get 50% off!');
}

Both run the exact same way, but most people would find the second example easier to read. Grouping conditions with parentheses is optional, but strongly preferred if it makes your code more readable. This will help you understand your program later on when you come back to it.

Comments:

Ad: