|
Operators
Operators Operators are the symbols used to work
with variables. You're already familiar with operators from
simple arithmetic; plus and minus are operators. While both x++ and ++x add one to x,
they are not identical; the former increments x after the
assignment is complete and the latter before. For example, if x
is 5, "y = x++" results in y set to 5 and x set to 6,
while "y = ++x" results in both x and y set to 6. The
operator "-" works similarly · If you mix numeric and
string values when adding two values together, the, result is a
string. For example, "cat" + 5 results in
"cat5".
Arithmetic Operators
Arithmetic Operators act on numbers.
Table given Below lists the arithmetic operators offered in
JavaScript and provides examples of each.
| Operators |
Used
For |
Example |
Equals |
| + |
Addition |
1+2 |
3 |
| - |
Subtraction |
15-10 |
5 |
| * |
Multiplication |
5*3 |
15 |
| / |
Division |
12/3 |
4 |
| % |
Modulus |
10%3 |
1 |
| ++ |
Increment |
X=8,X++ |
9 |
| -- |
Decrement |
X=7,X-- |
6 |
| - |
Unary
Negation |
-20 |
Negative
20 |
Assignment
operators
Operators used for assigning values to
variables. The table given below lists the various assignment
operators available in JavaScript.
| Operator |
Example |
Means |
| += |
X+=Y |
X=X+Y |
| -= |
X-=Y |
X=X-Y |
| *= |
X*=Y |
X=X*Y |
| /= |
X/=Y |
X=X/Y |
Comparison
Operator
Comparison Operators compare two
values and return either true or false.
| Operator |
Meaning |
Example |
| == |
is
equal to |
2==3
is False |
| != |
not
equal to |
2!=3
is True |
| > |
is
greater than |
2>3
is False |
| >= |
is
greater than or equal to |
2>=3
is False |
| < |
is
less than |
2<3
is True |
| <= |
is
less than or equal to |
2<=3
is True |
Logical
Operator
Logical Operators compare two
values and return either true or false.
| Operator |
Meaning |
if
X=7 and Y=2 then... |
| && |
AND |
(X=7)
&& (Y<5) is True |
| || |
OR |
(X=7)
|| (Y=7) is True |
| ! |
NOT |
X
!= Y is True |
Bitwise
operators
These treat the operands as bits rather
than numerical value.
The numerical values could be of any base: decimal, octal or
hex.
The bitwise operator will convert the operand to its binary
representation and accordingly return 1 or 0.
| Operator |
Action |
Example |
| & |
bitwise
AND |
10&3=2 |
| | |
bitwise
OR |
10|3=11 |
| << |
left
shift |
10<<3=80 |
| >> |
Sign-propagating
right shift |
10>>3=1 |
| >>> |
Zero-fill
right shift |
10>>>3=1 |
Bitwise
Assignment Operators
| Operator |
Example |
Means |
| <<= |
X<<=Y |
X=X<<Y |
| >>= |
X>>=Y |
X=X>>Y |
| >>>= |
X>>>=Y |
X=X>>>Y |
| &= |
X&=Y |
X=X&Y |
| ^= |
X^=Y |
X=X^Y |
| |= |
X|=Y |
X=X|Y |
JavaScript
Operators Order Of Precedence
| Action |
Operator(s) |
| Call,
Member |
(),[] |
| Negation/Increment |
!,
++, -- |
| Multiply/Divide |
*,
/, % |
| Addition/Subtraction |
+,
- |
| Bitwise
shift |
<<,
>>, >>> |
| Comparison |
<,
<=, >, >= |
| Equality |
==,
!= |
| Bitwise
AND |
& |
| Bitwise
XOR |
^ |
| Bitwise
OR |
| |
| Logical
AND |
&& |
| Logical
OR |
|| |
| Conditional |
?: |
| Assignment |
=,
+=, -=, *=, /=, %=, <<=, >>=, >>>=,
&=, ^=, |= |
|