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
trueorfalse. - Use
elsefor the fallback path. - Use
else ifwhen there are more than two paths.