A switch statement allows you to evaluate multiple conditions, checking for specific values.
Example:
let day = 3;
let dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
default:
dayName = "Unknown Day";
}
console.log(dayName); // Output: Wednesday
Explanation:
The switch statement checks the value of day.
If day is 3, it assigns "Wednesday" to dayName.
No comments:
Post a Comment