With numbers, strings, arrays, and objects,
we now got four super important types of values
that you will use a lot in JavaScript programming.
Now let's build up on that knowledge.
And one thing we haven't done too much,
or actually not at all up to this point,
is work with those values.
Sure, we use the alert command to output them,
but very often you want to do
calculations, transformations.
You want to do operations like that
with your data.
And therefore, I now want to explore
some important basics related to that with you now.
For this I'll delete these two alert lines here,
and now let's say we want to derive
for how many years this user,
which we are describing here, is an adult,
assuming that you become an adult
once you reach 18 years.
Now for that, we could add a new variable
that stores our result of that calculation,
which we could name adultYears.
Again, the name is up to you
as long as you follow the rules and the convention,
but the name of course also should convey some meaning
and describe the value which you will store
in this variable.
And here I plan on storing the number of years
for which this person here already is an adult.
Now, the interesting thing here now
is that now I don't want to hard code a new value.
Of course, I could store 14 in there
because I quickly calculated in my head
that 32 minus 18 is 14,
but that's not the idea here.
This input here could be coming from a user
through a form.
So even though we did hard coded here
as a developer for demo purposes,
this value 32
might actually not really be known by us,
the developer.
And therefore, we want to derive
the adult years dynamically.
We want to calculate them.
Also of course, to avoid the fact
that we make an error
whilst doing that math in our head.
So therefore, instead of just the writing 14 here,
I instead want to use my age,
which stores the 32,
and now deduct 18 from that age.
And you can do all those basic math operations,
which you know, in JavaScript as well.
You can deduct something by adding the minus symbol
and then the value you want to deduct,
18 in this case.
And now adultYears will store
the result of this operation.
And of course, this could also be a longer operation
with multiple steps.
So you're not just allowed
to deduct one value from another,
but you can add as many steps here as you want.
Of course, you're also not limited to subtraction.
You can also add values with the plus symbol,
multiply values with the asterisk symbol,
and divide by values with the forward slash.
But here we need the subtraction
and now adultYears should store the result of age,
which in this case is 32, minus 18.
And I will, again, alert this here.
I will alert adultYears,
save that, and on my website,
I therefore get 14 here in this alert box.
So that's working, and that's important.
This might sound obvious, and if it is, great,
but this is a key thing in programming, in general,
that you can, of course, not just use variables
to store values so that you can then output them,
but that you can also work with values
and do mathematical or other operations
on them to derive new values
with which you then might do something,
like in this case
outputting that new value again.
I also want to mention again
that variables are called variables
because the values stored in them
are allowed to change.
And at the moment that's never the case.
We always assign a value,
and then we leave it like that.
You can also reassign variables.
You're not limited to that initial value.
So for example, if the age would change
for some reason,
you can again reference age
and assign a new value
by adding the equal sign again.
And then you could store a value of, let's say, 45 here.
Now the important thing here for that is
that you don't add the let keyword again, though.
You don't do that.
You only add the let keyword
when you first create and introduce a variable.
Here, I did already create
and introduce that variable
and I'm then just assigning a new value,
and that is then done without let.
You only need let if you introduce
a new variable with a new name.
So here, for example, we need let.
We are using age again,
but only on the right side of the equal sign
for this calculation.
The variable, which we create, adultYears,
is brand new.
Here for age, on the other hand,
we are using an existing variable,
so we don't need let.
And now that I did change age here,
what do you think is the result of adultYears now?
Is it still 14 because it still uses 32
or will it be 27,
the result of 45 minus 18?
Well, let's see.
If I save that and it reloads, I see 27.
So it's the result of 45 minus 18.
And that should make a lot of sense
because instructions are executed step by step,
line by line, and therefore we did change age
before we use it in this calculation,
and hence the new value of age is used,
not the old value.
That wouldn't make too much sense.
So therefore, in this demo script,
we now never use the original age of 32.
We only use the new one.
And of course, we're still just playing around
with these features here
to get a feeling for what we can do
with JavaScript.