An if statement lets your program make a decision — run one block of code if a condition is true, and optionally a different block if it's false.
let temperature = 35;
if (temperature > 30) {
console.log("It's hot today.");
}<?php
$temperature = 35;
if ($temperature > 30) {
echo "It's hot today.";
}temperature = 35
if temperature > 30:
print("It's hot today.")let age = 15;
if (age >= 18) {
console.log("You can vote.");
} else {
console.log("Not old enough to vote yet.");
}<?php
$age = 15;
if ($age >= 18) {
echo "You can vote.";
} else {
echo "Not old enough to vote yet.";
}age = 15
if age >= 18:
print("You can vote.")
else:
print("Not old enough to vote yet.")Chain multiple conditions when there are more than two possible outcomes.
let score = 72;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 75) {
console.log("Grade: B");
} else if (score >= 60) {
console.log("Grade: C");
} else {
console.log("Grade: F");
}
// Output: Grade: C<?php
$score = 72;
if ($score >= 90) {
echo "Grade: A";
} elseif ($score >= 75) {
echo "Grade: B";
} elseif ($score >= 60) {
echo "Grade: C";
} else {
echo "Grade: F";
}
// Output: Grade: Cscore = 72
if score >= 90:
print("Grade: A")
elif score >= 75:
print("Grade: B")
elif score >= 60:
print("Grade: C")
else:
print("Grade: F")
# Output: Grade: COnly the first matching condition runs — once one branch is chosen, JavaScript skips the rest, even if a later condition would also have been true.