WEBVTT

1
00:00:00.440 --> 00:00:04.710
If you don't have your existing work
space open for this video, go ahead and

2
00:00:04.710 --> 00:00:06.430
launch it now.

3
00:00:06.430 --> 00:00:10.150
This switch statement is used for
multiple branching logic.

4
00:00:10.150 --> 00:00:13.410
So let’s skip over our two way branch and

5
00:00:13.410 --> 00:00:17.260
pull up the multi-way branch
an example for this video.

6
00:00:17.260 --> 00:00:20.900
Remember, we signed a value
to a variable called day.

7
00:00:22.200 --> 00:00:25.630
The we have several conditions for
the value.

8
00:00:25.630 --> 00:00:29.620
Let's go ahead and
comment out all these lines.

9
00:00:29.620 --> 00:00:34.640
So we can refer to it later and 
it won't interfere with our work.

10
00:00:34.640 --> 00:00:37.298
Switch is similar in some
ways to an if statement.

11
00:00:39.106 --> 00:00:46.410
It has parentheses, followed by curly
braces, which holds a block of code.

12
00:00:46.410 --> 00:00:48.920
The parentheses hold an expression.

13
00:00:48.920 --> 00:00:51.050
In this case, it's an if statement.

14
00:00:52.250 --> 00:00:57.147
Let's put in our variable day there,
and unlike an if statement,

15
00:00:57.147 --> 00:01:03.365
all cases are contained inside one block
of code, marked out with the keyword case.

16
00:01:05.152 --> 00:01:06.769
Let's write that case now.

17
00:01:14.828 --> 00:01:19.904
We're saying to the JavaScript
interpreter consider the value

18
00:01:19.904 --> 00:01:25.580
of day if equals 0,
execute this following block of code.

19
00:01:25.580 --> 00:01:27.620
When it reaches the break statement,

20
00:01:27.620 --> 00:01:32.410
it jumps out the entire block of code
to the next set of instructions.

21
00:01:32.410 --> 00:01:35.042
Let's go ahead and
add the statement we want to run,

22
00:01:35.042 --> 00:01:41.297
console.log ('Sunday').

23
00:01:44.730 --> 00:01:48.280
Now that we have one branch here,
let's add another.

24
00:01:48.280 --> 00:01:53.030
This is easy as simply copying and
pasting another case below.

25
00:02:00.753 --> 00:02:06.800
Now our program has two branches 
which can handle a 0, or a

26
00:02:06.800 --> 00:02:10.060
1 value in day.

27
00:02:10.060 --> 00:02:12.900
We can add as many cases
as we would like to.

28
00:02:12.900 --> 00:02:16.390
We're going to add five more to
handle the rest of the week.

29
00:02:16.390 --> 00:02:19.180
But first let me show you a common mistake

30
00:02:19.180 --> 00:02:22.310
you might make when writing
a switch statement.

31
00:02:22.310 --> 00:02:26.310
I'll remove the break
statement under Sunday.

32
00:02:26.310 --> 00:02:30.080
If we set the date to equal 0,
and run the code,

33
00:02:33.380 --> 00:02:36.350
you see it prints both Sunday and Monday.

34
00:02:37.720 --> 00:02:42.180
If the break is missing,
the statement will fall through

35
00:02:42.180 --> 00:02:45.770
to the next code, and
unexpected things can happen.

36
00:02:46.860 --> 00:02:50.240
This is a reason why many developers
consider the switch statement

37
00:02:50.240 --> 00:02:54.500
to be a bad choice to make when
branching inside a program and

38
00:02:54.500 --> 00:02:57.550
isn't considered a best practice.

39
00:02:57.550 --> 00:02:58.820
Let's add back the break.

40
00:03:01.478 --> 00:03:03.790
And then add the rest of our branches.

41
00:03:06.580 --> 00:03:10.010
Now we have seven branches for
any possible day of the week.

42
00:03:12.120 --> 00:03:17.461
But what happens if a day value
is other than 0 through 6?

43
00:03:18.860 --> 00:03:21.940
The switch statement also
provides a default option

44
00:03:23.910 --> 00:03:25.640
to catch those possibilities.

45
00:03:26.670 --> 00:03:32.400
This acts the way an else block acts,
in an if else statement.

46
00:03:33.550 --> 00:03:34.528
Here's what it looks like.

47
00:03:56.187 --> 00:03:58.327
Play with this a little bit now and

48
00:03:58.327 --> 00:04:03.220
see how it works in the same way
as our initial if else statement.

49
00:04:03.220 --> 00:04:05.760
That's really all there
is to a switch statement.

50
00:04:05.760 --> 00:04:09.140
If you're testing against
the value of a single variable,

51
00:04:09.140 --> 00:04:15.180
a switch statement saves you a lot of
typing with a lot of else if conditions.

52
00:04:15.180 --> 00:04:17.940
Let's move on to another way to branch.

53
00:04:17.940 --> 00:04:19.440
The ternary operator.
