Jonas: Let's now learn
about a couple of basic JavaScript operators.
So, here I already created the title for this lecture,
and also commented out the code here off the last lecture.
Okay, so we can actually do this here in different files,
like one file for each lecture.
But I prefer to just keep it everything simple here
all in one file.
Now, for operators, they can be
for example, the simple math operators
that we already know,
like plus and minus and times.
And then beside that,
in JavaScript we have a couple of other operators
that I'm also gonna show you.
So in simple terms, operators are like functions
that are written in a special way in JavaScript.
And in case you're wondering what functions are,
don't worry.
Again, we will talk about this a bit later.
But from now on, let's actually look at some math operators
and how we can use them in JavaScript.
So let's calculate in what year John was born,
and that's very simple.
So, let's call it yearJohn.
So we have 2018, which is the current year,
minus John's age, right?
And remember we declared it as 28,
so let's use the same here.
And so now we can analog it to the console.
So let's take a look at that.
And indeed, 1990.
So this here, the minus, is a math operator in JavaScript.
And let's now do the same for,
let's say for someone else.
Like John's friend,
let's say he's called Mark.
And so 2018 minus 33.
So mark is 33 years old.
And now you see that we are starting
to repeat some of the value.
So, for example, 2018 here,
we are using it multiple times,
and so let's declare it as a separate variable.
So that is what variables are for, okay.
So we can say var year is 2018.
And so now instead of having to reuse this 2018 here,
we can then right year in these two places.
Okay, so let's take a look at that.
And of course, it still displays the exact same number.
But if we wanted to change the year now,
let's say to 2020,
well then we wouldn't have
to change it here in these two places like we had before.
So before, if we wanted to change the year,
we would have to change it both here and here,
and that is something that we always
want to avoid in programming.
And so we defined this year in one place,
and then it gets used in these two places here.
Okay?
And actually let's also declare
all of these variables here in the same line
like I showed you before.
So year, then yearJohn,
and yearMark,
and then we move this vars from here,
so this just looks a bit better.
So that is just the minus operator here,
but of course we can do all kinds of math here.
So for example, we can multiplications.
So let's say we wanted to add two years here.
And so, "now" is not defined.
So this is of course not now, but it's year.
Well actually this should be called now
because now is the current year.
So 2018.
So what we want here is for this to be called now.
And also here, and here.
Alright, and so let's take a look at what happens then.
And indeed, it logs 2020 to the console,
which is 2018 plus two, right?
Of course we can also multiply.
So let's say now, times two.
And we can of course also divide.
So let's divide by 10 here, alright.
Logging it all to the console.
Well, then of course we see that this is 2018 times two.
And this is 2018 here divided by 10,
and so we end up with this decimal number here.
Okay.
So these are the math operators that we used here.
So let's add a comment here saying that.
And now I'm going to show you some logical operators.
So let's write it down here as well.
And so let's say that we wanted to compare the age
of Mark and John and figure out if,
for example, John is older than Mark.
And so for that we can use a logical operator.
So first of all, let's actually define these ages here
as different variables.
So ageJohn is 28,
and then ageMark is 33.
And so, of course, here we use ageJohn
and then down here ageMark.
So, let's create a new variable here
called var johnOlder.
And then we will say ageJohn greater than ageMark, okay.
And then, of course, we need to log it to the console here
to see our results.
And so let's take a look at what happens now.
And we see our result is this boolean, and it's false, okay.
And so what this means is that ageJohn,
which is 28 here,
is, of course, less than ageMark which is 33.
So basically here we asked the question,
"Is ageJohn greater than ageMark?"
and the result of that is false.
Okay, and so our johnOlder variable here
turns out to be false.
Now, if it was the other way around.
If we asked, ageJohn less than ageMark.
Well, then it should be the other way around.
And so yeah, indeed it is now true.
Again, because ageMark is 33 which is more than 28.
And so we have a way now
of basically creating a boolean variable
using one of the logical operators that I just showed you.
And there are many more that we're gonna explore
a bit later in the course, and actually
in this section in a couple of videos.
So, math operators here are pretty straightforward.
Then, these logical operators,
which are also very important,
as you're going to see later throughout the course.
And then, before we leave,
I also want to show you the typeof operator.
Okay, and this operator is a bit different
than the other ones.
So, the ones that we used up until this point
always required two things to compare
or to add or to multiply.
So now times two, or now divided by 10,
or ageJohn less than ageMark.
So we always had two operons.
But with the typeof operator, we only have one.
So let me show it to you.
So let's log to the console
the result of using the type of operator
on, let's say, the johnOlder variable.
Okay, and so basically what this will do
is to tell us the type of this variable.
And so indeed, we get boolean.
And that's because obviously this is a boolean variable.
So we saw it here as it returned this true.
And as we learned in one of the last lectures,
a true or false value is always a boolean.
Right, remember that?
So let's try it out with some other stuff here.
So typeof ageJohn, and let's now say ageJohn,
and this should be a number.
And indeed, it is.
Let's actually copy this now,
and let's try it out with a string,
and we don't have any here.
So let's just write it ourselves.
Mark is older than John, for example.
And then also, let's try something else.
So let's just declare a random variable here
and not define it.
And then, let's log to the console the type of this.
And so what do you expect that the type of x will be.
So let's test it,
and I hope you know that it will be undefined.
Okay, so that makes sense
because, indeed, we did not define any value
for the x variable.
And the one before here, of course, is a string.
So this may not seem like a big deal to you here,
this operator.
But actually you will see that we will use this here
in the future, and actually also later in this section.
Okay, so this is the very basics of JavaScript operators.
Now in the next lecture, we will keep working with operators
where I'm gonna show you some more,
let's say, advanced stuff.
So don't wait and come with me right to the next video.