Functions are a key feature of programming,
not just of JavaScript,
and functions are basically our own commands,
which we can define as a developer
to then execute them anywhere in our code.
And now it's time for our own custom functioner.
For this anywhere in your code,
you can add a new function by typing the function keyword.
So we saw the let keyword for defining variables,
now we have to function keyword for defining functions.
Why is it let and not variable? Well, it is what it is.
So here, we have the function keyword,
and as I said, with that, we can create our own command,
hence the next step is to give our own command a name,
and that name is totally up to you,
it's just, again, has to follow certain rules
and conventions, and these rules and conventions
are the same as for variable names.
However, there is an additional convention
that function names should not describe what's in them,
but instead, what this operation does.
Since a function is a command that you can later reuse
to perform a certain operation,
hence function names should describe as operation.
So here, I wanna calculate my adult years
and hence I'll name this function calculateAdulYears.
Now this naming might still be a bit strange
if you have never worked in programming before,
but it is something you will get used to.
Now, that's the name of our own function, our own command.
Now, things change a bit though.
We don't use the equal sign as we did it for a variables,
instead, for functions, we now add opening
and closing parentheses, and then, curly braces,
opening and closing curly braces.
And now, between those curly braces,
we put our operation code,
so the code that makes up this operation.
And we can also put something
between those parentheses here,
but I'll come back to that later.
Now, typically, you do now add a new line,
so you add a line break
and you indent this year a little bit for readability sake.
You don't have to, you can write it all in one line,
you also don't have to indent,
but it's the common notation to use multiple lines
and to indent because that will make it more readable.
And in here, you can now perform the operation,
which you wanna outsource.
In my case, that's this calculation
where I deduct 18 from age.
Now, for this, we can copy this code
and add in this function.
And now, we need to do something with it here.
Because like this, we performed this operation,
but out of the box, this just produces a new value,
which at the moment, would go into the white.
Now, one thing we could do here
is we could store this in adult years,
however, not by using let here, but without let,
and actually define this adult years variable
before we define this function.
Maybe like this, without an initial value,
which is also allowed.
And then here, down there,
I just assign a new value and hence I remove let.
Now, I'll come back to why I'm doing this in a second.
But for the moment, let's change our code like this.
Now, this function does custom command
overwrites sets a new value to adult years,
hence the new value is age minus 18.
So what I'm also doing down there.
Now, the interesting thing about functions is that,
actually, by defining such a function here,
as we're doing it here between lines 12 and 14,
does not execute the code in that function yet.
When this entire JavaScript code file is being parsed
and executed by the browser,
once the browser reaches this line
and this entire code block
therefore it will just memorize this function,
it will learn your own custom command so to say,
but it will not execute it yet.
Instead, you as a developer now have to tell the browser
when this function should be executed.
And we could do this here.
Instead of performing that operation
of which I wanted to get rid of as mentioned.
And to execute a function, you repeat its name,
so calculate adult years in my case,
and then you add parentheses,
opening and closing thereafter.
And that's some syntax which might look familiar to you
because that's what we're basically doing for the alert.
For this built-in command,
which turns out to be a built-in function
provided to you automatically by JavaScript and the browser.
Here, we also have a variable
between those opening and closing parentheses,
but I'll come back to why we have it here
and not here later.
For the moment here, for calculate adult years,
we don't need it.
And now, we can also calculate adult years here
instead of that operation, which we had before.
And now, the advantage of this
is that we only have this operation once in our function,
and here, I should also add a semicolon,
and then we just called this function into different places
where we wanna perform this operation.
And hence if we ever, for whatever reason,
need to change the logic in this operation here,
we can do it in this function instead of searching
for all the different places in our code
where we used this operation.
Here, of course, I wanna stick to the deduction though.
And with all of that, if we save this and reload,
we still see 14 and 27 as we did it before.
But now, we're utilizing our own function, our own command,
for writing the code once and then using it multiple times
in different parts of our code.
And that's the key benefit of functions.
I also like to call them code on demand.
We can define code, which is not executed immediately,
but which we instead trigger in different parts of our code
when we need that code to run.