Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,340 --> 00:00:08,140
You'll learn about let and const, another thing another construct you're going to see a lot in this
2
00:00:08,160 --> 00:00:11,080
course are arrow functions.
3
00:00:11,460 --> 00:00:15,890
That's a different syntax for creating Javascript functions.
4
00:00:15,960 --> 00:00:18,540
A normal javascript function of course looks like this.
5
00:00:18,540 --> 00:00:27,450
With the function keyword. You might also note a syntax where you have var myFunc equals function.
6
00:00:27,540 --> 00:00:30,230
Now an arrow function looks like this.
7
00:00:30,270 --> 00:00:34,160
Here I'm storing it in a constant and then on the right side of the equals sign.
8
00:00:34,290 --> 00:00:36,750
That's the arrow function syntax.
9
00:00:36,840 --> 00:00:42,540
A list of arguments here it's none but it could hold some arguments and then an arrow.
10
00:00:42,720 --> 00:00:47,280
So an equal sign and a greater than sign and then the function body.
11
00:00:47,280 --> 00:00:52,920
The arrow function snyntax is a bit shorter than the normal syntax since it omits the function keyword
12
00:00:53,550 --> 00:00:56,700
and it also and that is the great benefit.
13
00:00:56,700 --> 00:00:59,810
Solves a lot of the issues you often had with the.
14
00:00:59,810 --> 00:01:02,710
This keyword in javascript.
15
00:01:02,940 --> 00:01:10,290
If you've worked with javascript a bit, you probably know that the this keyword doesn't always refer to what
16
00:01:10,290 --> 00:01:15,180
you might have expected it to refer to during you writing your code.
17
00:01:15,180 --> 00:01:22,830
When you use this inside an arrow function it will always keep its context and not change it surprisingly
18
00:01:22,830 --> 00:01:24,450
on runtime.
19
00:01:24,450 --> 00:01:28,130
So let's now move to jsbin to see that arrow function syntax.
20
00:01:28,140 --> 00:01:31,320
In practice. Back in jsbin.
21
00:01:31,380 --> 00:01:37,980
let's create a normal function, printMyName like this.
22
00:01:38,890 --> 00:01:47,030
Now here we can of course output console log name and pass name as an argument.
23
00:01:47,220 --> 00:01:54,930
Now if I call printMyName like this and hit control enter, we get undefined because it didn't pass
24
00:01:54,930 --> 00:01:59,200
anything for it. We do the same with Max passed as an argument.
25
00:01:59,250 --> 00:02:07,410
We get Max. Now the arrow function equivalent is to store it in as a constant named printMyName.
26
00:02:07,410 --> 00:02:09,630
It could also be created with let.
27
00:02:09,660 --> 00:02:15,410
If you plan on re-assigning this variable printMyName as a constant.
28
00:02:15,520 --> 00:02:22,450
And now don't forget to add this arrow between the list of arguments and the function body. If you now clear
29
00:02:22,480 --> 00:02:24,610
and rerun this you'll still see Max.
30
00:02:24,610 --> 00:02:27,630
So here it behaves exactly the same way.
31
00:02:27,640 --> 00:02:30,820
Now the this keyword thing is something you'll see throughout the course.
32
00:02:30,850 --> 00:02:35,060
It becomes important once you add functions to objects of course.
33
00:02:35,140 --> 00:02:40,960
First of all let me show you some alternatives to this syntax though regarding the argument list.
34
00:02:40,960 --> 00:02:48,370
To be precise. If you only receive one argument as we do here, you can also use a shortcut of omitting
35
00:02:48,430 --> 00:02:49,850
the parentheses around it.
36
00:02:50,020 --> 00:02:56,720
That's only valid for exactly one argument though, not for more and not for less. Here,
37
00:02:56,740 --> 00:03:04,170
You will still get the same result. If you had a function which receives no arguments.
38
00:03:04,180 --> 00:03:06,270
This is not valid syntax.
39
00:03:06,340 --> 00:03:11,750
You need to pass an empty pair of parentheses like this.
40
00:03:11,860 --> 00:03:15,600
If we execute this now we also still see Max.
41
00:03:15,610 --> 00:03:19,080
So this works if you have more than one argument.
42
00:03:19,090 --> 00:03:20,740
You also need parentheses.
43
00:03:20,740 --> 00:03:23,410
So name and age like this won't work.
44
00:03:23,470 --> 00:03:26,950
You will need to wrap this in parentheses.
45
00:03:26,950 --> 00:03:33,550
And now you could output name and age and pass both as arguments here.
46
00:03:33,910 --> 00:03:38,310
So these are two different syntaxes you might see regarding the arguments.
47
00:03:38,380 --> 00:03:40,000
There also is an alternative.
48
00:03:40,000 --> 00:03:41,870
Regarding the function body.
49
00:03:41,950 --> 00:03:47,780
So on the right side of the arrow. Obviously a lot of functions just return something.
50
00:03:48,010 --> 00:03:58,920
So let's say here we actually want to multiply something and we get a number as an argument.
51
00:03:58,920 --> 00:04:08,490
Now we want to return number * 2. Obviously what we can do is we can console log the result of
52
00:04:08,490 --> 00:04:09,180
multiply.
53
00:04:09,180 --> 00:04:12,690
So what's returned by multiply and pass two here.
54
00:04:12,870 --> 00:04:16,620
If I now clear this and run this we should see four and we do.
55
00:04:16,740 --> 00:04:24,960
If you have this case where all you do in your function body is return and you have no other code in
56
00:04:24,960 --> 00:04:35,010
there you can omit the curly braces and write this in one line and then you also have to omit the return
57
00:04:35,010 --> 00:04:39,670
keyword. This is a very short version of writing this function.
58
00:04:39,690 --> 00:04:44,820
It gets a bit shorter if we take advantage of the shortcut of removing the parentheses around the single
59
00:04:44,820 --> 00:04:46,260
argument.
60
00:04:46,260 --> 00:04:50,310
And now what this does is it still returns the result of this code.
61
00:04:50,310 --> 00:04:54,230
Here we just omit the return keyword and we have 2 omitted.
62
00:04:54,450 --> 00:05:00,480
And we have a very concise and short way of writing a function that takes one or more arguments and
63
00:05:00,480 --> 00:05:01,860
returns something.
64
00:05:01,860 --> 00:05:05,690
So here clearing and running this still yields 4.
65
00:05:06,090 --> 00:05:11,430
So this is all the syntax you might see and the arrow function in general is something you'll see a lot.
66
00:05:11,430 --> 00:05:13,540
You saw the various syntaxes.
67
00:05:13,650 --> 00:05:16,020
You don't have to remember all of them right now.
68
00:05:16,050 --> 00:05:21,240
Just be aware that there are different syntaxes and if we then use them they'll quickly come back
69
00:05:21,240 --> 00:05:24,960
to your mind and you know why we use a given syntax.
7015
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.