CS133JS Beginning Programming: JavaScript

Selection Using switch case statements

 

Topics by Week 
1. Intro to JavaScript programming6. Arrays
2. Functions, Operators and Expressions7. Objects
3. Conditional Statements: if and switch8. Methods
4. Loops9. DOM
5. Midterm10. Final
11. Final 

 

Table of Contents


Introduction

Q and A

 

Review

Selection Using the Ternariy Operator, ?

A ternary expression evaluates to a value.

 

Selection Using if Statements

An if statement determines which branch of code will be executed.
Parts of an if statement:

Types of if statements

Braces (optional, but recommended, unless there is more than one statement to execute, then they are required.)

Exercise

There are three prices for a book titled The Joy of JavaScript:

Write the code and run it in the console so you can simply assign values to variables for input.


Selection using switch case statements

Parts of a switch statement

Multiple cases can be used to execute one statement

Multiple statements can be executed in each case

Exercise

Write code in the console to determine the price of The Joy of JavaScript using a switch statement instead of a multi-branching if else statement.

Reference

W3Schools

Switch Statement

 


Creative Commons License Beginning JavaScript Lecture Notes by Brian Bird, 2018, revised are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.