Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed JavaScript Basics!
      
    
You have completed JavaScript Basics!
Preview
    
      
  JavaScript has a particular operator to test more than one condition. It's called a logical "AND" operator, represented by two ampersands (&&).
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
                      Sometimes, a single condition
isn't enough to make a decision.
                      0:00
                    
                    
                      For example,
                      0:03
                    
                    
                      even if it's a hot day, you don't wanna go
swimming if you don't know how to swim.
                      0:04
                    
                    
                      In this case, you might ask,
if it's a hot day and I know how to swim,
                      0:08
                    
                    
                      then I'll go swimming.
                      0:13
                    
                    
                      There are two tests here.
                      0:14
                    
                    
                      Is it a hot day and do I know how to swim?
                      0:16
                    
                    
                      In order to move on to the action going
swimming, they both have to be true.
                      0:19
                    
                    
                      If the first condition is false,
for example, it's not a hot day,
                      0:24
                    
                    
                      then no action is taken.
                      0:28
                    
                    
                      If the second condition is false,
I don't know
                      0:30
                    
                    
                      how to swim It doesn't matter if it's
a hot day, I won't be going swimming.
                      0:33
                    
                    
                      JavaScript has a special operator
to test more than one condition.
                      0:37
                    
                    
                      It's called an AND operator and
is represented by two ampersands.
                      0:41
                    
                    
                      You place one condition on either
side of the two ampersands.
                      0:46
                    
                    
                      The two conditions
are evaluated separately, but
                      0:50
                    
                    
                      work together to create a single
Boolean value, true or false.
                      0:53
                    
                    
                      For example,
                      0:57
                    
                    
                      let's say you want to test if a variable
holds a number between 20 and 30.
                      0:58
                    
                    
                      Well, there's no single comparison
operator you can use to check if a number
                      1:03
                    
                    
                      lies within a specific range.
                      1:07
                    
                    
                      However, you can use the logical AND
operator for this,
                      1:09
                    
                    
                      I'll demonstrate using
the JavaScript console.
                      1:12
                    
                    
                      You can follow along too, if you'd like.
                      1:15
                    
                    
                      In the console, I'll declare
a variable named age and set it to 25.
                      1:17
                    
                    
                      I can test the value in
this variable like this.
                      1:22
                    
                    
                      What we really have here
are two separate conditions.
                      1:30
                    
                    
                      20 is less than age and
age is less than 30.
                      1:35
                    
                    
                      Each condition is tested separately and
produces its own true or false value.
                      1:40
                    
                    
                      We know that the value in
the age variable is 25.
                      1:45
                    
                    
                      So the first test asks,
if 20 is less than age.
                      1:49
                    
                    
                      In other words, is 20 less than 25?
                      1:53
                    
                    
                      Yes, it is.
                      1:55
                    
                    
                      So this first condition is true.
                      1:56
                    
                    
                      The second test asks, if age is
less than 30, is 25 less than 30?
                      1:59
                    
                    
                      Yes, it is.
                      2:04
                    
                    
                      So this condition is also true.
                      2:05
                    
                    
                      The role of the logical AND
operator is to combine the two tests.
                      2:08
                    
                    
                      It asks, is condition one true and
is condition two true?
                      2:13
                    
                    
                      In this case, yes, they are both true.
                      2:18
                    
                    
                      So I wanna run this by pressing Enter.
                      2:20
                    
                    
                      You can see that the result
is a true value.
                      2:23
                    
                    
                      Let's continue with a few more examples.
                      2:26
                    
                    
                      I'll change the value of
the age variable to 35,
                      2:29
                    
                    
                      then press the up arrow key twice
to bring back the conditions.
                      2:33
                    
                    
                      Now the first condition is true,
because 20 is less than 35.
                      2:39
                    
                    
                      But the second condition,
35 is less than 30 is false.
                      2:45
                    
                    
                      So notice how the entire
condition evaluates to false.
                      2:50
                    
                    
                      All right,
how about I set the value of age to 10?
                      2:55
                    
                    
                      Now the first condition is false and
                      3:01
                    
                    
                      the second condition is true,
because 10 is less than 30.
                      3:03
                    
                    
                      But it doesn't matter since
just one of these is false,
                      3:08
                    
                    
                      the entire condition evaluates to false.
                      3:13
                    
                    
                      Finally, let's say there's
a string value in the variable.
                      3:16
                    
                    
                      Well, that isn't a number at all.
                      3:23
                    
                    
                      So it can't be compared to a number.
                      3:25
                    
                    
                      In this case, both tests are false.
                      3:28
                    
                    
                      And logically enough,
the entire condition evaluates to false.
                      3:30
                    
                    
                      As you can see, the AND
operator is pretty strict.
                      3:36
                    
                    
                      Use the and
                      3:40
                    
                    
                      operator whenever you need two conditions
to pass before starting an action.
                      3:40
                    
                    
                      For example,
say you have a login form on your website.
                      3:45
                    
                    
                      You might check to see
that both the username and
                      3:48
                    
                    
                      password fields are filled out
before submitting the form.
                      3:50
                    
                    
                      If both the username and password fields
are empty, then don't submit the form.
                      3:53
                    
              
        You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up