30 Days Of JavaScript

Day 3: Logic

Lesson 2: The AND operator

And here's an expanded version where we check a range of temperatures

In this final example you can see that we've had to expand the middle if statement to do two checks temperature > 15 && temperature <= 28. Let's break it down a bit:

  1. First it will evaluate temperature > 15

  2. Then it will evaluate temperature <= 28

  3. Then the results of these will be evaluated with the && operator. This is the 'AND' operator and will return true if the things on both sides of it are true. If they are both false or just one is true then it will return false.

So if the temperature is 22 then:

  1. temperature > 15 will be true

  2. temperature <= 28 will be true

  3. true && true will be true

So the if statement will execute the code in between its { } brackets

If we try with temperature = 2 then

  1. temperature > 15 will be false

  2. temperature <= 28 will be true

  3. false && true will be false

The if statement won't execute the code between it's { } brackets because the result of the code in between it's ( ) brackets is false.

Outline

Go Pro?

If you upgraded to pro, sign in here

  • About
  • Blog
  • Privacy
Looking to email me? You can get me on my first name at allthecode.co