Operators are one of the most important parts of JavaScript.
If you want to create something and if you want to calculate something or if you want to do any kind
of operations.
Operators are required.
We have basically arithmetic operators.
Those are addition, subtraction, multiplication and division.
And for addition, we use a plus sign.
And for subtraction we use the minus sign.
And for multiplication, we use the star sign.
And for division, we use the forward slash sign.
Assignment operator is basically an equal to sign.
So basically you assign something to something else.
For example, here we have a value called A plus B, and the result of that, we are storing inside
another variable called result.
Now here we are using the assignment operator to assign all of these values to the result.
There are multiple ways how you can work with the assignment operators.
For example, you have a score and you are assigning that score to the value of ten.
So you'll be basically writing score is equal to score plus ten.
One of the easiest way to write this is simply specifying score plus equal to ten.
There is no mandatory rule that you have to use only this one or this specific one.
You can choose which one is easier to use.
The same way we have options for other operators like plus equal to minus, equal to multiplication,
equal to and division equal to.
We have operator precedence.
So basically in JavaScript what happens is the value for the multiplication is given first and then
the value for the other operators are given next.
For example, if we have a variable called result and inside that we are storing ten plus ten multiplied
by five, which should be the answer actually 100.
But we don't get that 100 here we get 60.
That's because the multiplication happens first.
So we have ten into five, which is 50, and then it adds up to the ten.
So that becomes 60 there.
So if you want the answer to be exactly how you want it and you want the addition to happen first,
then you will have to put it inside the parentheses.
So that would be like ten plus ten.
Put that in parentheses and then you're multiplying that with the five.
So what happens here is the ten plus ten gets added first and then it gets multiplied by five, and
then those values are stored inside the 100, the same way we have comparison operator, where we can
compare one variable to another variable.
So if you have a is equal to five and B is equal to ten, a lot of people forget that is equal to is
an assignment operator and they use it like this.
So if you do this, what happens is when you say A is equal to five and B is equals to ten, but in
the if statement, when you specify A is equal to B, the value of B is now changed to A and now by
default, it is going to be true.
It's not going to show you an error.
It's not going to show you any kind of debug message or anything.
It's simply will stop running.
So in this way this particular code becomes an bug.
So make sure that you use the assignment operator properly.
And this is a assignment operator, not a comparison operator.
So here we will have to do the comparison.
For example, here we have a is equals to five and B is equal to ten.
When we compare this use, the double equal to sign A is equal to B, obviously the answer will be false
because A is not equal to B, We have another operator with a triple equal to sign.
So basically we are checking whether A is equal to B, So here, yes, it is going to be false because
A is not equal to B most of the times.
Let's just say you said the A is equal to five and you said the B in with the string option with five
inside it.
And if you use this, it's still going to be false.
Let me show you an example of how this works.
Okay.
Here I'm inside my code.
I'm going to specify Var.
A is equals to Phi and I'm going to set var.
B is equal to.
I'm going to put it in quotations.
I'm going to set it as five.
And let me close this and let's create a simple comparison.
Operator So I'm going to say if and here if I specify that A is equal to B, and then inside this I'm
going to specify the answer with the console dot log.
And I'll give the answer as true.
So let's see if this works.
I'm going to save this go to.
Copy path.
Paste it in the browser and hit refresh.
Now if I right click and inspect, you can see there is a console tab here.
Just click on that one and you can see that it is giving it as true.
So let's do the same thing when I specify the three equal to sign if I assign this basically is going
to check whether A and B are strictly equal to each other.
So if I save this and now I'm going to go back to my browser and if I refresh, it doesn't give me any
answer.
That's because I've not given any else option.
So let's write some else statement else.
Say console, dot log.
Is.
False.
I'll save this one if I get back here and I hit refresh, you can see it says false.
Now what happens is if I remove just one operator here, and if I save this, and if I get back to my
browser and hit refresh, that's going to be true.
Because in double equal to sign, what happens is we are basically checking whether the values are equal
to or not.
Yeah, in this case, both are equal.
But when I use the triple equal to sign, we are basically asking the program to check the value strictly
so strictly whether they are equal to each other or not.
So in this case, if I get back and hit refresh, that's going to set it as false in the same comparison.
Operator we have, whether it is not equal to a specific value, for example, we have a is equal to
five and B is equal to ten.
And if I say here that if A is not equal to B, that's going to be true because here A is not equal
to B and we're using this strict comparison, then you'll be using a double equal to sign with a exclamatory
mark.
So that is going to be also as true.
There are some other comparison operators.
For example, we have here, we check whether A is greater than B, whether we check A is lesser than
B, A is greater than or equal to B, whether it is lesser than or equal to B, the same way we have
logical and or operator.
So most of the times, if you want to compare a several things, let's just say you have around like
7 to 8 different values which you want to compare.
So basically we are going to check like this.
We will say that if A is equal to B and C is equal to D, then we are going to write something.
So what happens here is we are comparing both the options together.
So let's just say if I want to skip one and I want to check only one of it is true, then I'm going
to use or method.
So it's going to be like if A is equal to B or C is equals to D, the program is going to run.
Let me give you an example.
Let's just say if the username and password is correct, then we'll let the user login.
In the order method.
If the user name is a name or an email, we will let the user log in the same way we have a modulus,
but in modulus, instead of dividing or multiplication, we are checking the remainder.
For example, I just create an error called 2003 and if I want to check the leap year of that particular
year, then you will be writing to the modulus of four.
So that will give me a remainder with three.
The same way we have increment and decrement.
For example, instead of writing a is equal to a plus one, we can write a plus or equal to one or a
plus plus it is exactly the same as plus plus a or the same thing goes for the subtraction also.
But there's a quite difference when it comes for prefix and postfix.
So in my case, I'm using a prefix here.
So what happens is when you create a variable A is equal to Phi, and if you set the prefix option that
is plus plus A, the answer would be six.
But if you use a postfix, it's going to result in two.
Phi.
Now what's happening here is when you're setting plus plus a a prefix is already added.
That means one is already added to the particular variable.
So your answer would be six.
But if you say postfix, it's going to still give you five.
But the next time when you create something, it's going to give you six.
Now that we have looked into the if statement the same way we have ternary operator, ternary operator
usually gets assigned with the question mark.
So I'm going to show you how this ternary operator works.
So let's just say we have a player, one with a score of 500 and we have a player two with a score of
600, and then we have a variable set to high score.
So right now I do not have any value for the high score because I want to check that.
So here I'm going to write an if statement that if player one score is greater than player two, then
we set the high score to the player.
One else will set the high score to the player.
Two This is using the if statement.
Now we can do the same thing with the ternary operator.
So we will set the variable high score and here we are going to check whether the player one has the
high score greater than player two if this condition is met to true, then we will take player one as
the high score or we will take the player two as the high score.