Let's talk about assignment operators in JavaScript, which includes the following.
So back to Visual Studio code.
The first one is what we call the assignment operator, and I'm going to paste the description with
some case study.
So over here I'm going to add a comment and that is the assignment operators.
And the first one is to use the equal to sign.
And it says that this is used to assign value to a variable.
And this is what we have used from the beginning of this course till this point.
Anytime we want to assign a value to a variable, we use the assignment operator.
And this is the most common use operators in JavaScript.
So let's look at the case study.
Let's say that you purchased ten apples from the grocery store.
How can you use JavaScript to track of how many apples that you have?
So here is going to be as simple as ABC by declaring a variable called total apples.
And is equal to ten.
As you can see here, that we have the underline.
And one thing that I did not tell you is that in JavaScript, you cannot really declare a variable that
has been declared already.
If you scroll up here, we have the same variable name being declared.
So we cannot reassign, so we can change this one to my total apples or something different.
So let me change this one to my total apples and I will use camel casing here.
And if you console log, my total apples is going to be ten.
Then the next operator is to use the plus equal sign.
So I'm going to copy this.
Assignment operator using plus and then the equal sign.
And we have used this one under the arithmetic operators.
So let's see how we are going to use.
First step is let me provide the description so that you can follow along pretty easily.
This is used to add value on the right to the variable on the left and then assign the result to the
variable on the left.
Let's have some case study here so that you better understand.
And this is the case study.
As the mayor of a small town, the current population of your town is 100.
But during this year, an additional 50 people moved to your town.
How would you calculate the total population now?
And here we go.
Let's look at the variable names here.
So here we have population.
So we can say that let the population equal to 100.
And this year it has increased by 50.
So now we can redeclare this variable as plus equal to 50.
And if we console log the population, let's have a look.
And we have 150.
But if you use const one more time, you're going to have error because we cannot re update a variable
declared using const.
And the next type of the arithmetic operator is using the minus.
And let me copy this.
And down here, paste it and change this one to minus.
And I'm going to be the opposite way.
So let's look at the case study so that you better understand.
You have $200 in your wallet, and after buying a book for $30, how can you calculate how much money
you now have in your wallet?
So you can pause this video and give it a shot.
Otherwise, let's continue.
So based on this, we can deduce the variables names here as money in wallet and is equal to 200.
And if I have used $30, how am I going to calculate?
And for this we have a couple of ways we can use something like this as let.
Money in wallet.
Let me say money in wallet left is equal to 200 -30.
And if I console log.
Money in wallet and that is the money in wallet left.
We have 117 but if you use the minus and equal sign is going to be as this money in wallet minus equal
to 30.
And it is same as before.
So if you console.log money in wallet, we have won 70.
Cool.
Right?
And the next type is the using multiplication sign and then the equal sign and you're going to be the
same format.
So let's look at the case study here.
And it goes like this.
You have 12 cookies and you decided to break each one into three pieces to share with your friends.
How would you calculate the total number of cookie pieces that you have now?
And it's going to be using the multiplication and then the equal sign.
And for this I'm going to declare a variable called cookies in Java.
So here cookies in Java is equal to 12.
And I will say cookies in Java is I'm going to update that one using multiplication sign equal to and
multiply by three.
So with this one, if I save it and check cookies in Java.
Is it?
Cookies?
Enjoy.
And we have 36.
Great.
Or I can multiply this one by three.
And I can remove this.
The result is the same, but this is the shortest syntax.
All right, cool.
So let me undo and bring back to the.
This one.
All right.
And next one is the division assignment.
And you're going to be assessed.
The difference here is we use different symbols before the equal sign.
So for this, let's provide this scenario.
It says that you have 200 page book that you want to study over the next five days, planning to read
the same number of pages each day.
How would you calculate how many pages you need to read each day?
And we're going to be division, right?
So here is going to be total pages.
And remember, we have this variable name up here as total pages.
So now we can change the name here as my total pages.
And is equal to 200 and total pages.
That is my total pages.
I have to change this one to use let because we are updating a divided and equal to five.
And if I console log my total pages, we have 40.
Then we have the remainder or the module operator for this one too.
We have explained it.
So let's provide the case study and let's have a look.
It says that you have 25 concert tickets that you want to distribute equally among four friends.
How would you calculate the number of leftover ticket when you hear leftover meaning the remainder?
Therefore, we are going to use the module operator and it goes like this let ticket and we have that
one already and is equal to 25.
I think we don't have ticket here being declared so we can go ahead and use this.
And next is we are going to use or update the ticket module then divided by four.
And if you console log ticket, we have one remainder of one.
The last one is the exponential for this.
One example is two exponent three meaning to multiply itself three times.
And let me provide the case study here as that in a math problem you are giving a base number of two
and ask to raise this number to the power of four.
How will you calculate this?
Meaning that we are going to multiply two four times, meaning two exponent four.
So we are going to use JavaScript to achieve this and it's going to be as let the base number equal
to two.
And we are going to use the exponent symbol and we're going to be as base and multiply two times is
equal to four.
And for this one, we're going to have 16.
So this one means that to multiply by two, multiply by two, multiply by two, that is four times.
Great.
So now if I save this and check inside here, that is base.
We have 16.
Great.
So guys, these are examples of the assignment operators.