So obviously thus far, we already have a lot of new pieces of information about functions.
What if I would tell you that you can also create functions inside of functions?
For example here in the sum up function, we could add a new function, again with any of the three function
syntaxes you learned,
I'll again use the arrow function syntax but that's not required, where I add a validate number function,
something like that.
Now this takes a number as an input, let's maybe name it
number and then does something.
Now it's a normal function, the only special thing is that it's part of another function.
Now if you think about it, it kind of makes sense though because what are functions? Essentially objects
and of course you can create an object and store it inside of a function, then it's locally scoped to
this function because of the block scope. Well nothing else is happening here, we're creating a function
inside of a function
and the only difference to all our other functions is that it's now only available inside of this function.
All other functions here essentially were created globally,
right?
I always did it on the root level of my script and therefore thanks to a normal scoping,
these were global scoped and we could use them everywhere.
Now I'm simply creating a function which can only be used in this function
and here we could simply check if number is not a number and if that's the case, replace it with zero and
otherwise keep it, then we could use validate number here to basically add our validated number here
to the sum. If we now reload, it works just as before
but a difference will be if we add something which is not a number, some text here for example, the result
will be different than before obviously because I removed the number but we get no error, without that
function we would get one.
But it's not so much about the logic I added here,
you can argue if that makes sense or not,
the important thing is that this is now a function which is only available inside of this function. Now
should you therefore only create functions inside of functions?
It depends.
Most of the time, in the vast majority of cases, global functions are the thing which you want
because the way functions work and how you build your code with different functions that depend on each
other,
you most often have a scenario where you want to work with a lot of global functions and where a function
itself might not really call a function that only belongs to it but you will also have some use cases
where this make sense and therefore it's nice to know that this exists. We'll dive a bit deeper into
functions created inside of functions later when we dive into more of the advanced aspects, there have
a look at performance and how the arguments and data you use in there actually behaves. For now it's
just something I wanted to show and make you aware with, as I mentioned, if you think about it, it actually
makes sense because functions are just objects which we store in a variable essentially here, in a
constant and therefore of course, just as we can store any other data in a function and scope it to it,
the same as possible with function themselves.