Documentation

Student-friendly HTML, CSS, and JavaScript reference with examples

Sections

If Statement Basics

If statements let you run code only when a condition is true.

if (condition) {
  // This code runs only if condition is true
}

Setting Default Values

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';
}

Example

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.