Test Multiple Conditions With the || Operator4:04 with Guil Hernandez
There are times you'll want to perform an action if either of the two possible conditions is true. In this video, you'll use the logical OR (|| ) operator to test multiple conditions.
Try to keep your conditional statements short and precise. When conditionals become too long or complicated, they can be challenging to track and manage. In some circumstances, it may be unavoidable.
A good rule of thumb is: if you find yourself creating a lengthy and overly complicated conditional, it might be worth taking the time to work out a more explicit, alternative approach.
You learned to use the and operator to perform an action of two or 0:00 more conditions evaluate to true. 0:03 There are times you'll want to perform an action if either of two possible 0:06 conditions are true. 0:09 For example, if it's hot or the indoor pool is open, then I'll go swimming. 0:10 In this case, either condition can be true. 0:16 It might be really cold outside, but the indoor pool is heated and warm, so 0:18 I'll go swimming. 0:22 On the other hand, the indoor pool may be closed but it's hot so 0:23 I'll go swimming in the ocean. 0:27 To run this type of test, you use the or operator. 0:29 It's called or because the entire condition returns true if either 0:33 condition one or condition two evaluates the true. 0:38 The logical or operator is represented by two pipe characters. 0:41 On most keyboard, the pipe characters is on the same key as the backslash. 0:46 Hold down the shift key to type it. 0:49 All right, let's see how the or operator works. 0:52 Say you let someone agree to the terms and 0:54 service of your website by typing either yes or just the letter Y. 0:57 They can type either one or other. 1:01 Back in the console, I'll store the response in a variable named agree and 1:03 assign it the string yes. 1:09 I can then use the logical or operator like this. 1:11 Agree strictly equals yes or 1:15 agree strictly equals y. 1:19 Just like with the logical and operator, 1:23 there's really two separate conditions here. 1:26 Each condition gets tested separately and produces its own true or false values. 1:29 The value in the agree variable is yes. 1:34 So the first test asks if yes is strictly equal to yes, it is. 1:37 So this first condition is true. 1:42 The second test asks if yes is strictly equal to y, this is not true. 1:45 The role of the or operator here is to ask if condition one is true or 1:51 condition two true. 1:56 In this case, one is true and 1:58 that's enough this entire condition evaluate to true. 2:00 Let's continue with two more examples. 2:05 I'll change the value and agree to just y. 2:07 Now the first condition is false, y is not equal to yes, 2:13 but the second condition y is equal to y is true. 2:18 So the entire condition is also true. 2:22 Remember, just the one test needs to pass. 2:24 What if I set the value of agree to n? 2:29 The first condition is false, and the second condition is also false. 2:34 In this case, because neither condition is true, 2:38 the whole condition evaluates the false. 2:41 Keep in mind that you need to create complete conditions on either side of 2:44 the or operator. 2:48 For example, you can't do something like this. 2:49 Even though it feels like this should be correct, 2:53 it really reads like, if agree is equal to yes or y. 2:57 Whatever appears on either side of the or operator has to be a complete condition. 3:01 Y by itself isn't really condition. 3:05 So remember, you need to have full conditions on either side. 3:08 In the logical end and or operator examples, 3:13 I've used just two conditions and a single logical operator. 3:15 You can actually have more than one of each operator. 3:20 For example, you can test if condition one is true, and condition two, 3:23 and condition three, and so on. 3:28 There's no limit to the number of logical and operators you string together. 3:30 Just remember that with the and operator, every single condition must be true for 3:34 the entire condition to evaluate to true. 3:39 If just one of the conditions is false, then the entire condition is false. 3:41 You can also string together multiple logical or operators. 3:46 In this case, only one of the conditions needs to be true for 3:49 the entire larger condition to evaluate to true. 3:53 And this example, if any of these three variables contains an empty string, 3:56 then the entire condition is true. 4:00
You need to sign up for Treehouse in order to download course files.Sign up