Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,160 --> 00:00:04,440
Now, besides the variables and constants,
2
00:00:04,440 --> 00:00:08,580
an ever crucial JavaScript concept are functions,
3
00:00:08,580 --> 00:00:10,740
which can either be created
4
00:00:10,740 --> 00:00:13,080
with help of the function keyword
5
00:00:13,080 --> 00:00:15,780
or with help of this arrow function syntax,
6
00:00:15,780 --> 00:00:17,880
to which I'll get back in a couple of seconds.
7
00:00:17,880 --> 00:00:20,070
Let's start with the function keyword.
8
00:00:20,070 --> 00:00:22,260
The idea behind functions,
9
00:00:22,260 --> 00:00:24,660
no matter how you are creating them,
10
00:00:24,660 --> 00:00:27,030
simply is that you're defining some code
11
00:00:27,030 --> 00:00:29,310
that's not executed immediately,
12
00:00:29,310 --> 00:00:32,070
but instead, at some point in the future
13
00:00:32,070 --> 00:00:34,050
when you call the function
14
00:00:34,050 --> 00:00:37,140
and that can be executed as often as you want
15
00:00:37,140 --> 00:00:40,140
because a function can be called multiple times.
16
00:00:40,140 --> 00:00:41,220
Now, to show you what I mean,
17
00:00:41,220 --> 00:00:42,930
let me finish this function.
18
00:00:42,930 --> 00:00:45,810
And this is how you write a basic function.
19
00:00:45,810 --> 00:00:49,410
You give it a name which comes after the function keyword.
20
00:00:49,410 --> 00:00:51,660
Then you have a so-called "parameter list",
21
00:00:51,660 --> 00:00:53,340
to which I'll get back in a second.
22
00:00:53,340 --> 00:00:56,910
You do create it with a opening and closing parenthesis
23
00:00:56,910 --> 00:00:59,160
and you need these parenthesis here
24
00:00:59,160 --> 00:01:02,040
and then you have opening and closing curly braces.
25
00:01:02,040 --> 00:01:04,170
And between those curly braces,
26
00:01:04,170 --> 00:01:06,390
you put your function code.
27
00:01:06,390 --> 00:01:09,060
So, the code that's not executed right now
28
00:01:09,060 --> 00:01:11,280
but at some point in the future.
29
00:01:11,280 --> 00:01:12,690
So, for example, here,
30
00:01:12,690 --> 00:01:15,480
I could put a console log statement
31
00:01:15,480 --> 00:01:16,740
into this greet function,
32
00:01:16,740 --> 00:01:20,040
and if I would save this and reload this page,
33
00:01:20,040 --> 00:01:22,860
you would see nothing in the console.
34
00:01:22,860 --> 00:01:26,040
The reason for that is that this function is defined
35
00:01:26,040 --> 00:01:27,750
but never executed.
36
00:01:27,750 --> 00:01:28,890
Because as mentioned,
37
00:01:28,890 --> 00:01:30,750
with a function, you're defining code
38
00:01:30,750 --> 00:01:33,150
that's not executed immediately,
39
00:01:33,150 --> 00:01:37,110
but at some point in the future when you call the function,
40
00:01:37,110 --> 00:01:39,660
when you invoke the function.
41
00:01:39,660 --> 00:01:43,200
And you do that by using the name of the function like this
42
00:01:43,200 --> 00:01:46,860
and then adding another pair of parenthesis after that name
43
00:01:46,860 --> 00:01:49,593
but without the function keyword in front of it.
44
00:01:50,460 --> 00:01:52,530
With the function keyword in front of it,
45
00:01:52,530 --> 00:01:54,300
you're defining it like this,
46
00:01:54,300 --> 00:01:56,070
you are calling it.
47
00:01:56,070 --> 00:01:59,880
And now, when this code is encountered in your code file,
48
00:01:59,880 --> 00:02:03,300
the code in the function will be executed.
49
00:02:03,300 --> 00:02:05,400
So, now with this added, if I save this
50
00:02:05,400 --> 00:02:06,990
and if I reload this page,
51
00:02:06,990 --> 00:02:09,360
now, I do see "Hello!" here
52
00:02:09,360 --> 00:02:11,940
because now we're calling the function.
53
00:02:11,940 --> 00:02:12,900
And, as mentioned,
54
00:02:12,900 --> 00:02:15,990
I can call the function as often as I want.
55
00:02:15,990 --> 00:02:20,160
You can literally add as many function invocations here
56
00:02:20,160 --> 00:02:22,740
as needed, and if you save that,
57
00:02:22,740 --> 00:02:26,853
you'll therefore see "Hello!" being output five times.
58
00:02:28,140 --> 00:02:32,070
Because I have five function invocations here.
59
00:02:32,070 --> 00:02:35,490
That's how you define and call a function.
60
00:02:35,490 --> 00:02:37,590
And this is a super important concept
61
00:02:37,590 --> 00:02:40,350
which we'll use all the time throughout this course.
62
00:02:40,350 --> 00:02:43,380
Also, especially, in React, it turns out
63
00:02:43,380 --> 00:02:47,220
that we'll define a crucial React building block
64
00:02:47,220 --> 00:02:48,330
as a function,
65
00:02:48,330 --> 00:02:50,850
but you'll learn more about this later,
66
00:02:50,850 --> 00:02:53,280
once we dive into React.
67
00:02:53,280 --> 00:02:56,310
Now, functions cannot just be defined and executed
68
00:02:56,310 --> 00:02:57,143
like this.
69
00:02:57,143 --> 00:02:59,730
Instead, they also take so-called "parameters",
70
00:02:59,730 --> 00:03:01,980
input values.
71
00:03:01,980 --> 00:03:05,460
For example, "username" and "message".
72
00:03:05,460 --> 00:03:08,190
You can add as many parameters as you want.
73
00:03:08,190 --> 00:03:10,680
You simply separate them with a comma.
74
00:03:10,680 --> 00:03:15,060
These parameters are then available inside of the function
75
00:03:15,060 --> 00:03:16,890
and only there.
76
00:03:16,890 --> 00:03:20,337
So, here I could then output "username"-
77
00:03:21,630 --> 00:03:23,137
Whoops.
78
00:03:23,137 --> 00:03:23,970
With a capital "N".
79
00:03:23,970 --> 00:03:26,727
And then, thereafter, also "message".
80
00:03:28,230 --> 00:03:31,380
I can do this because I'm accepting these two parameters
81
00:03:31,380 --> 00:03:32,850
and since it's your function,
82
00:03:32,850 --> 00:03:34,920
these parameter names are up to you,
83
00:03:34,920 --> 00:03:37,050
but they have to follow the same rules
84
00:03:37,050 --> 00:03:39,390
as these variable names did.
85
00:03:39,390 --> 00:03:40,350
And the same, by the way,
86
00:03:40,350 --> 00:03:42,270
is also true for the function name.
87
00:03:42,270 --> 00:03:44,640
This should also be defined just as you learned it
88
00:03:44,640 --> 00:03:45,900
for the variable names.
89
00:03:45,900 --> 00:03:46,733
For example,
90
00:03:46,733 --> 00:03:48,840
if it's a word that consists of multiple words,
91
00:03:48,840 --> 00:03:52,170
you would also use this CamelCase notation here.
92
00:03:52,170 --> 00:03:54,960
So, now this function takes two input parameters.
93
00:03:54,960 --> 00:03:57,480
Therefore, when you are calling this function,
94
00:03:57,480 --> 00:04:01,980
you also should provide values for these input parameters
95
00:04:01,980 --> 00:04:02,813
because in the end,
96
00:04:02,813 --> 00:04:04,560
the idea behind using parameters
97
00:04:04,560 --> 00:04:07,590
simply is that you can have one reusable function
98
00:04:07,590 --> 00:04:11,760
that can be reused with different input values.
99
00:04:11,760 --> 00:04:13,290
For example, here, I could call it
100
00:04:13,290 --> 00:04:17,238
with "Max" and "Hello!" the first time
101
00:04:17,238 --> 00:04:20,440
and then call it again where I greet Manuel
102
00:04:21,339 --> 00:04:25,710
and say, "Hello, what's up?" like this.
103
00:04:25,710 --> 00:04:27,300
Now, I'm using the same function
104
00:04:27,300 --> 00:04:29,280
with different input values.
105
00:04:29,280 --> 00:04:31,320
That's possible, thanks to parameters,
106
00:04:31,320 --> 00:04:33,420
and therefore, If I reload this,
107
00:04:33,420 --> 00:04:36,030
I see "Max" and "Hello!"
108
00:04:36,030 --> 00:04:38,997
and then I see "Manuel" and "Hello, what's up?"
109
00:04:40,140 --> 00:04:41,970
Now, what's also worth noting here
110
00:04:41,970 --> 00:04:46,080
is that you can also assign default values to parameters
111
00:04:46,080 --> 00:04:47,790
by adding an equal sign here.
112
00:04:47,790 --> 00:04:49,467
For example, "Hello!"
113
00:04:50,370 --> 00:04:53,550
This would allow me to call this function
114
00:04:53,550 --> 00:04:56,820
without providing a value for the second parameter.
115
00:04:56,820 --> 00:04:58,260
And if I don't provide one,
116
00:04:58,260 --> 00:05:01,020
the default value will be used.
117
00:05:01,020 --> 00:05:04,560
But I can, of course, also override that default value
118
00:05:04,560 --> 00:05:07,923
by simply providing a value for that parameter here.
119
00:05:09,060 --> 00:05:10,737
With that, if we reload this,
120
00:05:10,737 --> 00:05:14,680
I get "Max" and "Hello!" thanks to this default here
121
00:05:15,840 --> 00:05:18,330
and then I get "Manuel" and "Hello, what's up?"
122
00:05:18,330 --> 00:05:22,263
where I do use this value for overriding the default.
123
00:05:23,580 --> 00:05:26,460
Now, functions cannot just take input values
124
00:05:26,460 --> 00:05:28,770
but they can also return values
125
00:05:28,770 --> 00:05:30,333
by using the "return" keyword.
126
00:05:31,290 --> 00:05:33,030
So, for example, in this function,
127
00:05:33,030 --> 00:05:35,970
instead of console log in username and message,
128
00:05:35,970 --> 00:05:38,850
I could return a string that combines both.
129
00:05:38,850 --> 00:05:40,530
I could return a string here,
130
00:05:40,530 --> 00:05:45,300
where I say, "Hi I am", a white space here,
131
00:05:45,300 --> 00:05:47,640
and then I add the "userName"
132
00:05:47,640 --> 00:05:51,789
and then another white space or dot maybe,
133
00:05:51,789 --> 00:05:53,703
and then the message.
134
00:05:55,410 --> 00:05:57,660
So, now with that, I'm using this function here
135
00:05:57,660 --> 00:05:59,640
to, in the end, construct a string
136
00:05:59,640 --> 00:06:02,160
based on these input values.
137
00:06:02,160 --> 00:06:03,870
And we could also use "return"
138
00:06:03,870 --> 00:06:06,690
if we would not accept any parameters.
139
00:06:06,690 --> 00:06:08,490
In this case, I'm using the parameters
140
00:06:08,490 --> 00:06:10,620
for deriving the returned value,
141
00:06:10,620 --> 00:06:11,760
but if you have a function
142
00:06:11,760 --> 00:06:14,730
that doesn't need any input in order to work correctly,
143
00:06:14,730 --> 00:06:17,070
you of course also don't have to accept one.
144
00:06:17,070 --> 00:06:19,650
Parameters and the "return" keyword
145
00:06:19,650 --> 00:06:21,690
work totally independent from each other,
146
00:06:21,690 --> 00:06:23,190
but, as in this example,
147
00:06:23,190 --> 00:06:24,783
they can also work together.
148
00:06:25,770 --> 00:06:29,640
So, with that, this function here no longer greets a user,
149
00:06:29,640 --> 00:06:31,980
but instead creates a greeting.
150
00:06:31,980 --> 00:06:32,813
And therefore,
151
00:06:32,813 --> 00:06:34,712
we also might want to rename it
152
00:06:34,712 --> 00:06:37,560
because whenever you name things,
153
00:06:37,560 --> 00:06:39,480
functions, variables, whatever,
154
00:06:39,480 --> 00:06:43,320
you should describe what's stored inside of it,
155
00:06:43,320 --> 00:06:45,690
in case of a variable or constant,
156
00:06:45,690 --> 00:06:48,810
or what it does in case of a function.
157
00:06:48,810 --> 00:06:51,630
So, here, since it no longer greets a user
158
00:06:51,630 --> 00:06:54,360
but instead creates a greeting string,
159
00:06:54,360 --> 00:06:57,747
we might want to rename it to "create greeting".
160
00:06:58,710 --> 00:07:00,570
And then when we call it down there,
161
00:07:00,570 --> 00:07:04,740
we, of course, also no longer get any output in the console.
162
00:07:04,740 --> 00:07:07,200
Instead, now, we should store the result
163
00:07:07,200 --> 00:07:08,820
in a constant or variable
164
00:07:08,820 --> 00:07:13,410
because this function now returns a value which we can use.
165
00:07:13,410 --> 00:07:17,943
We could use it as an input for console log, for example,
166
00:07:18,930 --> 00:07:20,700
and the offer, in that case,
167
00:07:20,700 --> 00:07:23,637
we would see, "Hi, I am Max. Hello!"
168
00:07:24,960 --> 00:07:27,750
Maybe we should add a white space after the dot here
169
00:07:27,750 --> 00:07:29,880
to improve the formatting.
170
00:07:29,880 --> 00:07:31,980
But since this here is not great
171
00:07:31,980 --> 00:07:33,930
from a readability perspective,
172
00:07:33,930 --> 00:07:37,530
we instead might want to store it in a constant
173
00:07:37,530 --> 00:07:40,650
or variable like this and then use that constant
174
00:07:40,650 --> 00:07:43,983
or variable so that the overall code is more structured.
175
00:07:45,450 --> 00:07:49,249
So, with that here, we could get our two greetings
176
00:07:49,249 --> 00:07:54,249
and then, of course, also, console log these two greetings.
177
00:07:55,170 --> 00:07:57,670
And therefore, we would see those greetings again.
178
00:07:59,250 --> 00:08:02,580
So, functions can also be used for producing
179
00:08:02,580 --> 00:08:04,680
and returning values
180
00:08:04,680 --> 00:08:06,210
and they should be named such
181
00:08:06,210 --> 00:08:09,090
that it's very clear what a function does.
182
00:08:09,090 --> 00:08:10,200
Though, in the end, of course,
183
00:08:10,200 --> 00:08:12,120
just as with the variable names,
184
00:08:12,120 --> 00:08:14,073
the function names are up to you.
14207
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.