Write a program that checks a variable called day and logs "It's the weekend!" if it’s either "Saturday" or "Sunday", otherwise logs "It's a weekday."
Control flow allows you to decide which pieces of code run based on certain conditions.
The if
statement runs code only if a condition is true
.
Example:
let age = 18;
if (age >= 18) {
console.log("You are an adult.");
}
else
runs if the if
condition is false
.
Example:
let time = 20;
if (time < 18) {
console.log("Good day.");
} else {
console.log("Good evening.");
}
Allows multiple conditions to be checked in sequence.
Example:
let score = 75;
if (score >= 90) {
console.log("A grade");
} else if (score >= 75) {
console.log("B grade");
} else {
console.log("C grade");
}
Used to select one of many code blocks based on a value.
Example:
let fruit = "apple";
switch (fruit) {
case "apple":
console.log("Apples are red or green.");
break;
case "banana":
console.log("Bananas are yellow.");
break;
default:
console.log("Unknown fruit.");
}