JavaScript 5 - Switch Statements
Projected Time
About 60 minutes
- 15 minutes for lesson
- 30 minutes for Independent Practice
- 15 minutes for Check for Understanding
Prerequisites
Motivation
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.
Objectives
Participants will be able to:
- Know when to use a switch statement instead of an if/else statement
- Write a switch statement using proper syntax
Specific Things to Learn
- when to use a switch statement
- switch syntax
Lesson
JavaScript 5 (Video walkthrough of lesson slides)
- Please watch the video the first time without working along with the demonstration. Just absorb the concepts. Then, you can watch the demonstration a second time and code with the instructor if you like.
Materials
JavaScript 5 - Switch Statements (slides)
Common Mistakes / Misconceptions
You need a break
statement at the end of each case
.
There is no comparison operator in a switch statement, but there is one in the if/else statement
Independent Practice
Techtonica staff will assign pairs.
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).
Challenges
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).
Supplemental Materials
Check for Understanding
- Write down the syntax of a switch statement in JavaScript.
- What will happen if break is not used in a switch statement?
- What is the advantage of using a switch statement over an if/else statement?
- Using a switch statement, write a program to accept a lower case letter. Create an alert with the text ‘This letter is a vowel’ if the letter is a vowel.
Extensions
Complete these HackerRank Challenges: