If Statement

An if statement runs code only when a condition is true.

Basic Pattern

const time = 9
 
if (time < 12) {
  console.log("Good morning")
} else {
  console.log("Good afternoon")
}

Boolean Example

const isStudent = true
 
if (isStudent) {
  console.log("You are a student")
} else {
  console.log("You are not a student")
}

Remember

  • The condition should evaluate to true or false.
  • Use else for the fallback path.
  • Use else if when there are more than two paths.