Hello and welcome to another new episode of
learning simplified. In this tutorial we are
going to understand the Math operators and
its various significance in JavaScript
programming. So let's get straight into project.
Here first of all let's have a variable
with a numeral value of 5, and we are going
to have another variable, let it be 3. Now
if
we say that var c equals to a + b, in general
this thing will emphasize -
document.write(c) - get back to our project
and reload. We can see that here comes the
result of this addition operation that we
are seeing over here, that means this 5 +
3 =
8 and that is been executed over here. In
this particular case we have declared it as
a
numeral, as a number directly and that is
why this addition operation is being carried
out but whenever it comes about retrieving
values of some input HTML elements it is
practically been retrieved as a string and
then it has to be converted into a number
and
that is why we needed those additional operations
over there. Ok. So now let's get back
to our project and similar to this addition
operation if we now use '-' and if we print
that into our project into our HTML Browser,
if we now reload we can see that the
difference is being executed over here which
is 2; that means 5 minus 3 = 2.
In the same manner if we want to multiply
here always use and Asterix sign in case of
carrying and order of multiplication. So now
this variable c it will now contain a value
which is a product due to the multiplication
of these two variables; variable a and
variable b. Press control + s and get back
to our project and reload; and we can see
that
here comes the operation here comes the final
result which is 15. In the same manner,
If we want to have a division; that means
5 here will be divided by 3; now let's get
back to our project, reload and we can see
that this is the exact value - 1.66666 and
then it is being cut off where's the nearest
whole number that we have seen over
here - 7.
Now in case if we want to show our remainders
that means 5 by 3, the quotient will be 1
but there will be a remainder of 2 and that
is what we are going to observe over here.
That is why we will be using a modulus operator.
This percentage value it actually
executes a modulus operator. So if we now
Press control + s, get back to our project
we
can see that the remainder is 2 that means
5 / 3 the quotient is one but the remainder
is 2 and that is what which is being executed
over here. In case if we place it as 21
and reload we can see that there is no remainder;
21 is directly divisible by 3 and that
is why there won't be any remainder and that
is what it is executing over here which is
zero.
Now for say we are having a number over here
21 which is stored against the variable a;
we want to increase this number by 1 value
and that the safest syntax for it will be:
var a = a + 1; now if we just print this value.
We can see that the value is 22 instead
of 21 as we have written down over here. But
this is the actual value with which a '+1'
is getting added to. And then the new value
is getting stored against the same variable
that we had used in our previous case. Now
in this particular case we can use another
method with which there should be an increment
by 1 the value that has been assigned
against a variable and the final result will
be shown in to your web page. So in that
particular case we can safely conclude that
a++; now if we press Control + s and get
back to our project and reload, we can see
that the exact same value is been executed
over here.
So in case of a +1 increment, we can see that
there will be an increment to the value of
the final result and it will be assigned against
the same variable that we have assigned
and it will be shown in to your page. In the
same procedure if we place here 'a--', get
back to your project and reload, you can see
that the same thing will be observed over
here which is 20. Now what will happen if
we want to increment Our value by the number
that we want to add to our variable; for say
if we want to increase number by 10 or by
11 - so if we place here this one and get
back to our project and reload, we can see
that a +11 has been added to this, as we can
see over here. 21 + 11 equals to 32. So in
this particular case we can observe that an
11 is been added to this previous value of
21 and then the final result of the variable
a is been assigned against 32 and the final
result is getting observed into your HTML
page as we can see over here - it is executing
32. In the same manner if we want to subtract
this value from our previous value that we
have provided, all we need to do we need to
provide a 'a-=', get back to your project
on
reload and you can see that 21 -11 equals
to 10.
In the same manner if we want to have a multiplication
to this of 3 times, all we can do
we can place here or multiplication assignment
operator; here it goes, press control + s,
get back to your project and you can see that
21 X 3 equals to 63. And the new variable
is now assigned to 63 and it is now executed
over here. In the same manner if we want a
division to be carried out; we can see that
a division is now executed over here; the
result is 7 and in that particular case all
we need to do we need to provide this '/'
sign over here.
So this is the JavaScript math operator that
we have learnt from this tutorial. Hope you
guys have liked this tutorial; if you guys
have liked this video then do not hesitate
to
hit that red subscribe button down below.
Hope to see you guys in our next tutorial.
Till then, bye.
