JAVASCRIPT Tutorial

If-Else Statements

Key Concepts:

  • Condition: A Boolean expression that evaluates to true or false.
  • True Block: The code executed if the condition is true.
  • False Block: The code executed if the condition is false.

Steps:

  1. Check the condition: Evaluate the Boolean expression in the condition.
  2. Execute the true block: If the condition is true, execute the statements in the true block.
  3. Execute the false block: If the condition is false, execute the statements in the false block.

JavaScript Example:

// if the age is less than 18, print "You are not old enough."
if (age < 18) {
  console.log("You are not old enough.");
} 
// otherwise, print "You are old enough."
else {
  console.log("You are old enough.");
}

Additional Notes:

  • The if block is only executed if the condition is true.
  • The else block is executed only if the condition is false.
  • The else block is optional, but the if block is not.
  • Multiple else if statements can be chained after the if statement to check multiple conditions.