1
00:00:02,190 --> 00:00:07,270
So obviously thus far, we already have a lot of new pieces of information about functions.

2
00:00:07,320 --> 00:00:11,720
What if I would tell you that you can also create functions inside of functions?

3
00:00:11,760 --> 00:00:18,300
For example here in the sum up function, we could add a new function, again with any of the three function

4
00:00:18,300 --> 00:00:19,950
syntaxes you learned,

5
00:00:19,980 --> 00:00:27,270
I'll again use the arrow function syntax but that's not required, where I add a validate number function,

6
00:00:27,300 --> 00:00:28,700
something like that.

7
00:00:28,700 --> 00:00:32,730
Now this takes a number as an input, let's maybe name it

8
00:00:32,730 --> 00:00:35,900
number and then does something.

9
00:00:35,920 --> 00:00:40,870
Now it's a normal function, the only special thing is that it's part of another function.

10
00:00:40,870 --> 00:00:47,080
Now if you think about it, it kind of makes sense though because what are functions? Essentially objects

11
00:00:47,080 --> 00:00:53,230
and of course you can create an object and store it inside of a function, then it's locally scoped to

12
00:00:53,230 --> 00:00:58,150
this function because of the block scope. Well nothing else is happening here, we're creating a function

13
00:00:58,150 --> 00:00:59,440
inside of a function

14
00:00:59,440 --> 00:01:04,990
and the only difference to all our other functions is that it's now only available inside of this function.

15
00:01:05,140 --> 00:01:08,350
All other functions here essentially were created globally,

16
00:01:08,350 --> 00:01:08,620
right?

17
00:01:08,620 --> 00:01:13,390
I always did it on the root level of my script and therefore thanks to a normal scoping,

18
00:01:13,390 --> 00:01:16,620
these were global scoped and we could use them everywhere.

19
00:01:16,630 --> 00:01:20,260
Now I'm simply creating a function which can only be used in this function

20
00:01:20,260 --> 00:01:25,540
and here we could simply check if number is not a number and if that's the case, replace it with zero and

21
00:01:25,540 --> 00:01:33,750
otherwise keep it, then we could use validate number here to basically add our validated number here

22
00:01:33,750 --> 00:01:37,910
to the sum. If we now reload, it works just as before

23
00:01:38,010 --> 00:01:44,370
but a difference will be if we add something which is not a number, some text here for example, the result

24
00:01:44,400 --> 00:01:48,540
will be different than before obviously because I removed the number but we get no error, without that

25
00:01:48,540 --> 00:01:49,640
function we would get one.

26
00:01:49,980 --> 00:01:51,900
But it's not so much about the logic I added here,

27
00:01:51,900 --> 00:01:53,660
you can argue if that makes sense or not,

28
00:01:53,730 --> 00:01:59,310
the important thing is that this is now a function which is only available inside of this function. Now

29
00:01:59,310 --> 00:02:02,420
should you therefore only create functions inside of functions?

30
00:02:02,460 --> 00:02:03,650
It depends.

31
00:02:03,660 --> 00:02:08,670
Most of the time, in the vast majority of cases, global functions are the thing which you want

32
00:02:08,670 --> 00:02:14,280
because the way functions work and how you build your code with different functions that depend on each

33
00:02:14,280 --> 00:02:14,880
other,

34
00:02:15,030 --> 00:02:21,480
you most often have a scenario where you want to work with a lot of global functions and where a function

35
00:02:21,480 --> 00:02:27,300
itself might not really call a function that only belongs to it but you will also have some use cases

36
00:02:27,300 --> 00:02:33,390
where this make sense and therefore it's nice to know that this exists. We'll dive a bit deeper into

37
00:02:33,390 --> 00:02:38,070
functions created inside of functions later when we dive into more of the advanced aspects, there have

38
00:02:38,070 --> 00:02:44,070
a look at performance and how the arguments and data you use in there actually behaves. For now it's

39
00:02:44,070 --> 00:02:49,860
just something I wanted to show and make you aware with, as I mentioned, if you think about it, it actually

40
00:02:49,860 --> 00:02:54,780
makes sense because functions are just objects which we store in a variable essentially here, in a

41
00:02:54,780 --> 00:02:59,880
constant and therefore of course, just as we can store any other data in a function and scope it to it,

42
00:03:00,090 --> 00:03:02,430
the same as possible with function themselves.
