If Statements & Operators

If statements

If statements are used to make decisions in code. They allow the program to take different actions based on conditions. The basic syntax of an if statement is:

     if (condition) {
    // code to be executed if condition is true
    }  
    

You can also include an "else" clause in an if statement to specify code to be executed if the condition is false:

     if (condition) {
    // code to be executed if condition is true
    }  else {
    // code to be executed if condition is false
    // think of it as "if not, do this"
    }
    

An else if statement is used in combination with an if statement. If the condition in the if statement is false, the code in the else if statement will be executed if its condition is true.

   if (condition) {
    // code to be executed if condition is true
    }  else if {
    // code to be executed if previous condition is false and this condition is true
    // if not, but this, do this
    }

Operators:

Operators are symbols that perform operations on values and variables. Some common operators in JavaScript include:

Logical Operators:
// Using && (and)
if (hour >= 14 && hour < 16) {
  // Do something when hour is between 14 and 16 (inclusive)
}

// Using || (or)
if (hour >= 16 || hour < 14) {
  // Do something when hour is either later than 16 or earlier than 14
}

// Using ! (not)
if (!(hour >= 14 && hour < 16)) {
  // Do something when hour is NOT between 14 and 16 (inclusive)
}
Comparison Operators:
// Using == (equal to)
if (hour == 14) {
  // Do something when hour is exactly 14
}

// Using != (not equal to)
if (hour != 14) {
  // Do something when hour is NOT exactly 14
}

// Using > (greater than)
if (hour > 14) {
  // Do something when hour is greater than 14
}

// Using < (less than)
if (hour < 14) {
  // Do something when hour is less than 14
}

// Using >= (greater than or equal to)
if (hour >= 14) {
  // Do something when hour is greater than or equal to 14
}

// Using <= (less than or equal to)
if (hour <= 14) {
  // Do something when hour is less than or equal to 14
}

And finally, here is an example that combines logical and comparison operators:

if (hour >= 14 && hour < 16 || hour == 9 && minutes == 30) {
  // Do something when hour is between 14 and 16 (inclusive), or exactly 09:30
}