Note: *Operators: operators are a set of character which have a specific meaning and work on either one or more values(known as operand) to produce another value.
+, -, *, /, %, ++, --
Yes, Quite familiar, aren't they ? The meaning is also same, All you need to do is to apply them to operands, the little unfamiliar it mays seem to you %, ++, -- operators, but they are not so much different. ++ and -- are increment and decrement operators, which means something+1 or something-1, let us see by example how all these work
var a = 5;var b = 4;console.log(a+b); //additionconsole.log(a-b); //subtractionconsole.log(a*b); //multiplicationconsole.log(a/b); //divisionconsole.log(a%b); //mod or say remainder after divisiona = b++;console.log(a); //post incrementa = ++b;console.log(a); //pre incrementb = a--;console.log(b); //post decrementb = --a;console.log(b); //pre decrement
Now, how is that?
first four needs no Introduction all easy arithmetic. But when you see Mod(%) it means the remainder after the division. That is no difficult either. Now we come to the increment(++) and decrement(--) operators. This means they are used as a shortcut for the increment, as
a = a+1 can be written as a++ or ++a
a = a-1 can be written as a-- or --a
but wait a moment ? what is the matter with writing before and after the variable, does that makes any difference? Yes it does, as you see in the output, in first case, we have the value of a=5; than we assign a b++ to it, and print it; Now it happens like it,
a = 5;b = 4;a = b++;
is equivalent to
a = b;b = b+1;
does that makes sense ? yes it is first assigned and then incremented and in other case
a = ++b;
is equivalent to
b = b+1;a = b;
same is the case for the decrement operators, so when we assign b to a with a post increment the value in a is still 4, but next time it is called it is already 5 and with a pre increment it becomes 6. same kind of thing happens when we do the decrement operations.
=, +=, -=, *=, /=, %=
The key of assignment operators we have already seen in previous example '=', yes the equal sign, is the assignment operator, but we do have some more short hand assignment operators, we call them shorthand because they are kind of shortcut. As you can see in the following example.
var a = 5;var b = 4;a=b;console.log(a);a+=b; //a = a+b;console.log(a);a-=b; //a = a-b;console.log(a);a*=b; //a = a*b;console.log(a);a/=b; //a = a/b;console.log(a);a%=b; //a = a%b;console.log(a);
As you can see in the example all they do is to reduce just a bit of coding effort.
&&, ||, !
These are the operators which evaluates to only true or false values. used in the control flow structures to decide the control of the program.
very simple example of these are
var a = true;var b = false;console.log(a && b); //needs to be both true to evaluate into trueconsole.log(a || b); //needs to be both false to evaluate into falseconsole.log(!a); //converts false into true or true into false
==, <, <=, >, >=, !=
These operators are used to compare two values and results into the true or false of the expression, the usage is same as of Logical Operators as shown in the following example
var a = 5;var b = 4;console.log(a==b); //false because a is not equal to bconsole.log(ab); //true because a is greater than bconsole.log(a>=b); //true because a is greater than b, not equal though, its like a>b || a==bconsole.log(a!=b); //true because a is not equal to b
This is not complete yet, we are going to show you about bitwise operators and we really want to show you some stuff in working and to make you understand those, we will give a complete article to them with a vary little part for other operators, which are left.