Switch Statements
๐จโ๐ผ When you're comparing one value against many specific possibilities, a
switch statement is cleaner than many if/else statements.The syntax:
switch (value) {
case 'option1':
// code for option1
break
case 'option2':
// code for option2
break
default:
// code if no case matches
}
- Create a variable
daywith a day name like"Monday" - Write a switch statement that logs:
- "Start of work week" for Monday
- "Midweek" for Wednesday
- "Almost there!" for Friday
- "Weekend!" for Saturday or Sunday
- "Regular day" for anything else
๐ฐ Don't forget the
break statement after each case, or execution will "fall
through" to the next case!Missing
break is a common bug! Each case needs break unless you
intentionally want fall-through behavior.