(refer to slides and MDN article)
- What is a switch statement, when would I use one?
- Why a switch statement over an if/else?
- What is the syntax of a switch statement?
- What is the “break” part of a switch statement, and what will happen if no break statement is used?
JavaScript is used on the frontend of almost every website. It's also a widely-used scripting language that be used on the backend as well. The JavaScript lessons set a solid foundation in JavaScript basics so we can use the language in more robust ways in later lessons.
- when to use a switch statement
- switch syntax
JavaScript 5 (Video walkthrough of lesson slides)
JavaScript 5 - Switch Statements (slides)
-
You need a
break
statement at the end of eachcase
. -
There is no comparison operator in a switch statement, but there is one in the if/else statement
- JavaScript switch statements (video)
- Switch statements (video) by FreeCodeCamp
- JavaScript switch statements (video)
- Switch statement docs (MDN)
If you have previously completed it, refer back to the calculator activity from the JavaScript 1 lesson. Refactor your code to replace your if/else statement(s) with switch statement(s).
If you have previously completed it, refer back to the movie night activity from the JavaScript 3 lesson. Refactor your code to replace your if/else statement(s) with switch statement(s).
Complete these HackerRank Challenges:
- HackerRank challenge: Switch which deals with a simple switch statement.
- Conditional Statements: Switch