The course is part of this learning path
- Learn the difference in structure between an if statement and a switch statement
- Learn what is the switch expression
- Learn how to create a case clause
- Learn how to create a default case
- Learn the use of the break statement in the switch statement
For this switch statement the expression will be the myCar variable. Inside of the curly brackets, I will write the first case clause. Case, in quotes Toyota, colon. Colon is the separator between the case and the block of code to be executed. And whatever code is underneath the colon is what's gonna be executed if the expression matches the case. Since I'm refactoring the if statement to be the switch statement, I'm gonna copy the console log under if my car triple equals Toyota, and paste it under the case.
Now comparing the first case statement with the first if statement, if my car triple equals Toyota is the same as the myCar expression being evaluated against the case Toyota, both will return false and neither will execute the corresponding block of code, which in this case is the console log.
Now, as part of the switch statement block of code, I'm gonna add a break. Break, semi-colon. And a break will stop the switch statement from continuing on to the next clause if the case statement evaluates to true. Now I'm gonna speed up the video for this portion of code. I need to repeat the process and make two additional case clauses for each else-if statement from above. With the current value of the expression and however switch statement is structured, none of the case clauses will evaluate true.
The if statement has an else statement that triggers when all previous conditions do not evaluate to true. The switch statement has something similar. That is called the default case. I will type default colon, and copy and paste the corresponding console log from the else statement above.
Now, because I wrote this as the final case, there is no need for me to add a break statement. Now I could put the default in any case position, including the first one. And if I were to do that, I would include the break statement. Now I will execute this. And in the console to the right, "I don't own that car" is displaying.
Now, one other thing about the break statement use case. In each of these case clauses, I'm using console log to display a message. When console log is executed, it does not break the execution cycle. Underneath the Toyota case, if I were to replace the console log with return, the break statement would no longer be necessary. And that's it. Thanks for watching, at Cloud Academy.
Farish has worked in the EdTech industry for over six years. He is passionate about teaching valuable coding skills to help individuals and enterprises succeed.
Previously, Farish worked at 2U Inc in two concurrent roles. Farish worked as an adjunct instructor for 2U’s full-stack boot camps at UCLA and UCR. Farish also worked as a curriculum engineer for multiple full-stack boot camp programs. As a curriculum engineer, Farish’s role was to create activities, projects, and lesson plans taught in the boot camps used by over 50 University partners. Along with these duties, Farish also created nearly 80 videos for the full-stack blended online program.
Before 2U, Farish worked at Codecademy for over four years, both as a content creator and part of the curriculum experience team.
Farish is an avid powerlifter, sushi lover, and occasional Funko collector.