Week 4 Day 2
New Terms
- Conditional Statements
- Switch Statement
- Break
- Default
Conditional Statements
At the heart of JavaScript's decision-making process are conditional statements. These allow a program to execute specific code blocks depending on whether a condition is met. The most common conditional statements include if, else if, and else, which evaluate expressions and run code accordingly.
Switch Statement
When dealing with multiple possible values for a variable, a switch statement provides a cleaner alternative to multiple if...else conditions. It evaluates an expression and executes the matching case block.
Break
The break statement is crucial in switch cases and loops. It halts execution of a loop or switch statement when a condition is met, preventing unnecessary code from running.
Without break, the program would continue executing subsequent cases even if a match was found. This can lead to unintended behavior.
Default
The default keyword in a switch statement acts as a fallback option when none of the specified cases match. It ensures the program can handle unexpected inputs gracefully.