1
00:00:02,170 --> 00:00:06,850
Well if you compare both, we have the function declaration, function statement which look like this and

2
00:00:06,850 --> 00:00:09,370
the function expression like this which I just showed you.

3
00:00:09,370 --> 00:00:15,460
Of course needless to say, in function expression form here, you can also still take arguments and still

4
00:00:15,460 --> 00:00:17,900
return values that didn't change.

5
00:00:17,950 --> 00:00:24,640
Now the important difference to what it really boils down here is that for this function declaration,

6
00:00:24,790 --> 00:00:32,710
function statement syntax, Javascript automatically hoists the function to the top and then also fully

7
00:00:32,710 --> 00:00:34,060
initializes it for you.

8
00:00:34,090 --> 00:00:40,750
So what I mentioned, Javascript reads the file and no matter where you defined your function, it acts as

9
00:00:40,750 --> 00:00:45,490
if it would be defined at the top of the file, which means you can actually call functions before you

10
00:00:45,490 --> 00:00:46,240
declare them.

11
00:00:46,240 --> 00:00:52,030
That can be convenient and if your code style is to always have the functions at the end of the file,

12
00:00:52,390 --> 00:01:00,520
then you have to use this approach. With function expressions, this hoisting is also working but the constant

13
00:01:00,520 --> 00:01:03,520
gets hoisted but it gets hoisted as undefined,

14
00:01:03,520 --> 00:01:06,920
something I also had a look at in this behind the scenes module.

15
00:01:07,120 --> 00:01:12,460
So it's basically not initialized so you can't call the function before you defined it and that's an

16
00:01:12,460 --> 00:01:13,580
important difference,

17
00:01:13,630 --> 00:01:19,010
you have to define, you have to initialize your functions before you try calling them

18
00:01:19,110 --> 00:01:23,840
and in my opinion this is a bit of a cleaner style, though you will find both code styles,

19
00:01:23,860 --> 00:01:27,470
I will say that, it's not strictly bad or worse than the other.

20
00:01:27,490 --> 00:01:29,960
I personally prefer this approach a little bit,

21
00:01:29,980 --> 00:01:35,860
I think being forced to define functions before you use them is a great thing but of course it's needless

22
00:01:35,860 --> 00:01:40,670
to say that you could also simply define functions with the left approach before you use them. So

23
00:01:40,670 --> 00:01:45,550
you don't have to use the right approach just because you want to do that, the right approach just forces

24
00:01:45,550 --> 00:01:48,770
you to define functions before you call them. Now

25
00:01:48,940 --> 00:01:51,830
ultimately it's therefore up to you which one you prefer,

26
00:01:51,880 --> 00:01:57,850
I am personally a fan of the right approach but as I said, both works and both can be used,

27
00:01:57,850 --> 00:01:59,620
there is no bad approach here.

28
00:01:59,620 --> 00:02:02,140
Just to also quickly demonstrate what I meant,

29
00:02:02,170 --> 00:02:04,560
here we have this function defined.

30
00:02:04,570 --> 00:02:12,280
Now if I would call start here before that definition, if I save that and reload, I get an error that

31
00:02:12,310 --> 00:02:17,820
we cannot access start before initialization and we wouldn't get that error

32
00:02:17,860 --> 00:02:21,660
if I had my function started let's say like that,

33
00:02:21,670 --> 00:02:26,570
so in this declaration, in this statement syntax. If I would do that,

34
00:02:26,680 --> 00:02:31,930
this would work just fine as I mentioned because Javascript reads the entire file and if it finds function

35
00:02:31,930 --> 00:02:37,170
declarations like this, it kind of acts like they were at the top of the file.

36
00:02:37,450 --> 00:02:40,240
So that is an important difference,

37
00:02:40,300 --> 00:02:41,430
as I mentioned this is the style

38
00:02:41,440 --> 00:02:44,830
I prefer but not a style you absolutely have to use necessarily.
