Student-friendly HTML, CSS, and JavaScript reference with examples
If statements let you run code only when a condition is true.
if (condition) {
// This code runs only if condition is true
}
A common pattern is to set a default value, then change it if a condition is met:
let message = 'default value';
if (score > 80) {
message = 'excellent';
}
let age = 20;
let status = 'minor';
if (age >= 18) {
status = 'adult';
}
// status is now 'adult'
The condition must be something that evaluates to true or false.