1
00:00:00,660 --> 00:00:05,800
This lesson is going to be about the JavaScript switch statement, and the name really gives it away,

2
00:00:05,850 --> 00:00:09,630
it gives you the ability to switch between different cases.

3
00:00:09,780 --> 00:00:13,890
And if the case is true, then you can run a block of code.

4
00:00:13,950 --> 00:00:14,670
Which statement?

5
00:00:14,670 --> 00:00:18,610
There's more information, of course, at the Mozilla Developer Network about the switch statement.

6
00:00:18,900 --> 00:00:23,710
So basically it evaluates an expression and finds the matching case.

7
00:00:23,730 --> 00:00:28,030
So for the sample here that they have, they have an expression.

8
00:00:28,050 --> 00:00:33,000
So this is a string equal to papayas and the switch matches the expression.

9
00:00:33,010 --> 00:00:38,880
So the expression goes within the rounded brackets and then you've got curly brackets running the block,

10
00:00:38,880 --> 00:00:41,850
a code checking to see all the different cases.

11
00:00:41,880 --> 00:00:47,400
So until that case translates to be true, then that's when it's going to start running the code.

12
00:00:47,580 --> 00:00:54,400
And notice as well that there are breaks in between that breaks that switch block of code.

13
00:00:54,630 --> 00:01:01,950
So if we were to run for papayas, papayas was up here and we had no break, we would run both blocks

14
00:01:01,950 --> 00:01:03,460
of code until we hit a break.

15
00:01:03,510 --> 00:01:05,700
There's also an option for a default.

16
00:01:05,910 --> 00:01:12,120
So when you run this, you can see that it outputs the correct corresponding value that's within the

17
00:01:12,120 --> 00:01:12,510
case.

18
00:01:12,600 --> 00:01:17,970
It gives you the option to make a cleaner conditional statement instead of having to put a bunch of

19
00:01:17,970 --> 00:01:18,380
else.

20
00:01:18,390 --> 00:01:22,110
If you also have the default as well that you can utilize.

21
00:01:22,260 --> 00:01:28,800
The challenge within this lesson is to select a response depending on the name of the person in the

22
00:01:28,800 --> 00:01:32,400
variable that's being called where we've got person.

23
00:01:32,400 --> 00:01:33,780
Person is Lowrance.

24
00:01:33,960 --> 00:01:39,120
The switch is taking that expression and comparing it with all the different cases so you can go ahead

25
00:01:39,120 --> 00:01:43,680
and pause the video, work through this example and I'll show you the solution coming up that we need

26
00:01:43,680 --> 00:01:49,110
to do is to find the person I'm going to use the same as within the example where we're going to be

27
00:01:49,110 --> 00:01:50,430
using the name Lawrence.

28
00:01:50,730 --> 00:01:54,660
And then we've got our switch statement taking in the expression.

29
00:01:54,660 --> 00:01:57,180
So this expression could be any type of variable.

30
00:01:57,420 --> 00:01:59,730
We could be also looking for a number as well.

31
00:01:59,850 --> 00:02:01,800
And then running through the switch.

32
00:02:02,100 --> 00:02:03,570
We're setting up the cases.

33
00:02:03,570 --> 00:02:06,770
So it's case and then the value of the case.

34
00:02:06,780 --> 00:02:08,640
So we'll take a look for John.

35
00:02:08,640 --> 00:02:12,750
And then here we've got the code that we're going to be running and you can come up with any type of

36
00:02:12,750 --> 00:02:13,910
message you want there.

37
00:02:13,920 --> 00:02:15,660
And then don't forget the break as well.

38
00:02:15,720 --> 00:02:19,740
We also have the case, so we're checking to see if equal to Lowrance.

39
00:02:19,740 --> 00:02:23,730
And if it is, then we've got the colon there and then we're going to run our block of code.

40
00:02:23,730 --> 00:02:28,890
And I'm also going to do one without a break and we'll add in the break afterwards so you can see that

41
00:02:28,890 --> 00:02:33,360
what happens if we don't include the break, add in the default as well.

42
00:02:33,370 --> 00:02:36,750
And I'll do a break here if none are true and we can just output.

43
00:02:36,750 --> 00:02:38,190
Nobody found refresh.

44
00:02:38,190 --> 00:02:38,820
Found it.

45
00:02:39,030 --> 00:02:40,590
And hi, Steve.

46
00:02:40,840 --> 00:02:44,910
That's because we don't have a break there, so we can go and we can add in the break.

47
00:02:44,940 --> 00:02:47,430
And actually the break should be between here.

48
00:02:47,550 --> 00:02:51,360
So the break should be just after the block of code that we want to run.

49
00:02:51,450 --> 00:02:55,740
So this gives us the option to add in as many cases as necessary.

50
00:02:55,870 --> 00:03:01,710
There's a lot cleaner than if we were looking for a person is equal to John else if person is equal

51
00:03:01,710 --> 00:03:05,900
to Lawrence, else if person is equal to Steve and so on.

52
00:03:06,150 --> 00:03:11,490
So it gives us is the section that you've all been waiting for and that's functions.

53
00:03:11,640 --> 00:03:15,720
And this is where we can really power up with what we do in JavaScript.
