Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,130 --> 00:00:03,840
So enough for hobbies
2
00:00:03,840 --> 00:00:07,710
and objects for now, let's explore control structures.
3
00:00:07,710 --> 00:00:10,140
I already mentioned the if keyword
4
00:00:10,140 --> 00:00:13,860
which is used to create so-called if statements before.
5
00:00:13,860 --> 00:00:18,860
The idea here is that you compare values, for example,
6
00:00:18,870 --> 00:00:21,480
and you then execute the code inside of the if statement
7
00:00:21,480 --> 00:00:24,333
only if this condition here is met.
8
00:00:25,740 --> 00:00:29,610
Now the if statement also allows you to add a else statement
9
00:00:29,610 --> 00:00:31,800
to define the code that should be executed
10
00:00:31,800 --> 00:00:34,470
if this condition is not met.
11
00:00:34,470 --> 00:00:36,930
And also else if statements if you want check
12
00:00:36,930 --> 00:00:39,933
for more conditions if the first condition wasn't met.
13
00:00:40,830 --> 00:00:45,330
And you can add as many else if statements here as you want,
14
00:00:45,330 --> 00:00:47,733
but only one else branch.
15
00:00:49,320 --> 00:00:52,140
Now of course, checking for hard-coded values like this
16
00:00:52,140 --> 00:00:53,790
makes absolutely no sense.
17
00:00:53,790 --> 00:00:56,760
This will always be true, for example.
18
00:00:56,760 --> 00:00:59,550
Instead, typically, of course, you'll use if statements
19
00:00:59,550 --> 00:01:03,150
to check content you don't know in advance.
20
00:01:03,150 --> 00:01:06,960
For example, here I could get some dummy password
21
00:01:06,960 --> 00:01:09,900
by using the built-in prompt function.
22
00:01:09,900 --> 00:01:12,180
This is a function provided by the browser
23
00:01:12,180 --> 00:01:14,583
which will prompt the user for input.
24
00:01:16,050 --> 00:01:18,780
You see here I get this overlay that opens up.
25
00:01:18,780 --> 00:01:21,300
Here I could type your password,
26
00:01:21,300 --> 00:01:23,310
and now I would get this overlay
27
00:01:23,310 --> 00:01:24,993
which asks me for a password.
28
00:01:26,310 --> 00:01:27,960
Now here I'm getting this overlay
29
00:01:27,960 --> 00:01:31,110
whenever I type a couple of characters in my code here,
30
00:01:31,110 --> 00:01:32,970
and since this is a bit annoying,
31
00:01:32,970 --> 00:01:37,970
I'll indeed go to the preferences here
32
00:01:38,130 --> 00:01:40,090
and then the CodeSandbox settings
33
00:01:42,660 --> 00:01:47,370
and set the preview settings to disable preview on edit
34
00:01:47,370 --> 00:01:50,850
so that it will not automatically re-execute the code
35
00:01:50,850 --> 00:01:52,863
after I typed a couple of characters.
36
00:01:54,540 --> 00:01:56,790
So with that, I'm now fetching this password,
37
00:01:56,790 --> 00:02:00,990
and I could now check if the password is equal
38
00:02:00,990 --> 00:02:02,130
using the triple equals
39
00:02:02,130 --> 00:02:06,903
and operator to hello, let's say, written like this.
40
00:02:08,070 --> 00:02:13,070
And if that's the case, I'll console log, hello works,
41
00:02:14,460 --> 00:02:17,940
else, if this condition here should not be met,
42
00:02:17,940 --> 00:02:20,760
if the password entered by the user is not hello,
43
00:02:20,760 --> 00:02:21,810
written like this,
44
00:02:21,810 --> 00:02:25,380
I could check if the password is maybe equal to hello,
45
00:02:25,380 --> 00:02:27,513
starting with a lowercase L,
46
00:02:28,350 --> 00:02:33,093
in which case, here, I could console log hello works.
47
00:02:34,680 --> 00:02:36,930
But also, if that's also not the case,
48
00:02:36,930 --> 00:02:39,900
I might want to execute this fallback code here
49
00:02:39,900 --> 00:02:43,023
where I say access not granted.
50
00:02:45,210 --> 00:02:47,010
With this, now if I save this,
51
00:02:47,010 --> 00:02:49,260
the code does get executed again,
52
00:02:49,260 --> 00:02:50,400
but I'll still cancel this
53
00:02:50,400 --> 00:02:53,940
and manually refresh to clear the console.
54
00:02:53,940 --> 00:02:58,110
And now if I enter hello with a capital L,
55
00:02:58,110 --> 00:03:02,220
and press okay in my console, I'll see hello works,
56
00:03:02,220 --> 00:03:05,520
because since I entered hello, written like this,
57
00:03:05,520 --> 00:03:10,173
this condition here was met and this code here was executed.
58
00:03:11,310 --> 00:03:16,110
On the other hand, this code and this code was not executed.
59
00:03:16,110 --> 00:03:19,260
That's why the if statement is called a control structure
60
00:03:19,260 --> 00:03:21,933
because it controls which code gets executed.
61
00:03:23,130 --> 00:03:25,080
If I reload this
62
00:03:25,080 --> 00:03:27,780
and I enter anything else like this here,
63
00:03:27,780 --> 00:03:31,710
and I press okay, I get access not granted,
64
00:03:31,710 --> 00:03:33,840
because of course I didn't never enter hello
65
00:03:33,840 --> 00:03:36,150
written like this nor written like this.
66
00:03:36,150 --> 00:03:38,673
Therefore, this code got executed.
67
00:03:39,810 --> 00:03:41,790
So that's the if statement.
68
00:03:41,790 --> 00:03:43,740
Another crucial control structure
69
00:03:43,740 --> 00:03:46,500
you should know about is the for loop.
70
00:03:46,500 --> 00:03:50,190
Now JavaScript knows different types of for loops,
71
00:03:50,190 --> 00:03:52,590
one very important kind of for loop
72
00:03:52,590 --> 00:03:55,710
we'll also see in this course is the for loop
73
00:03:55,710 --> 00:03:58,233
where we loop through an array.
74
00:03:59,340 --> 00:04:03,390
For that, I'll again create my hobbies dummy array here
75
00:04:03,390 --> 00:04:06,630
with sports and cooking, let's say,
76
00:04:06,630 --> 00:04:08,730
and now if I want to execute some code,
77
00:04:08,730 --> 00:04:11,040
for every entry in this array,
78
00:04:11,040 --> 00:04:14,910
I could create a for loop where I create a constant
79
00:04:14,910 --> 00:04:19,200
that will be recreated for every iteration of that for loop,
80
00:04:19,200 --> 00:04:21,750
because for loops are about executing
81
00:04:21,750 --> 00:04:25,740
the same piece of code multiple times, as you will see.
82
00:04:25,740 --> 00:04:28,200
And I could name this constant, hobby,
83
00:04:28,200 --> 00:04:30,600
because now with the special of keyword,
84
00:04:30,600 --> 00:04:34,230
I tell JavaScript that it should create a new constant
85
00:04:34,230 --> 00:04:36,630
for every item in this array,
86
00:04:36,630 --> 00:04:38,130
and it should execute the code
87
00:04:38,130 --> 00:04:41,100
between these curly braces as often as needed
88
00:04:41,100 --> 00:04:44,370
to go through all these elements in this array,
89
00:04:44,370 --> 00:04:47,250
so for this array, two times.
90
00:04:47,250 --> 00:04:50,400
So if I then console log hobby here, for example,
91
00:04:50,400 --> 00:04:55,400
this code, this console log code will get executed twice
92
00:04:55,470 --> 00:04:57,900
because I have two elements in this array.
93
00:04:57,900 --> 00:05:01,050
The first time for hobby being equal to sports,
94
00:05:01,050 --> 00:05:03,903
the second time for hobby being equal to cooking.
95
00:05:05,100 --> 00:05:07,440
If I save this
96
00:05:07,440 --> 00:05:09,930
and I reload here, enter anything here
97
00:05:09,930 --> 00:05:12,630
and open the console, I see sports
98
00:05:12,630 --> 00:05:14,160
and cooking being output here
99
00:05:14,160 --> 00:05:18,513
because of this line of code being executed multiple times.
100
00:05:19,680 --> 00:05:23,190
So this for of loop is also a kind of loop
101
00:05:23,190 --> 00:05:24,480
you should be aware of
102
00:05:24,480 --> 00:05:26,823
because we'll also see this in this course.
8210
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.