JavaScript operators are
symbols that are used to perform operations on operands.
1.
Arithmetic Operators.
2.
Assignment Operators.
3.
String Operators.
4.
Increment operators
5.
Comparison (Relational) Operators
6.
Special Operators
7.
Logical Operators.
8.
Bitwise Operators.
Arithmetic Operators
JavaScript
has the arithmetic operators like .
|
+ (Addition)
|
-(Subtraction)
|
* (Multiplication)
|
/ (Division)
|
% (Modulus)
|
|
Example of +
var a = 10 + 2;
|
Example of -
var b = 10 - 2;
|
Example of *
var c = 10*2;
|
Example of /
var d = 14/5;
|
Example of %
var e = 14%5;
|
|
Result:-
12
|
Result:-
8
|
Result:-
20
|
Result:-
2.8
|
Result:-
4
- the remainder of 12/5 in integer math is 4.
|
Assignment operator
The
assignment operator = assigns a value to a variable. such as strings and
numbers are assigned directly.
|
=
|
+=
|
−=
|
*=
|
/=
|
%=
|
|
(Simple
Assignment )
|
(Add
and Assignment)
|
(Subtract
and Assignment)
|
(Multiply
and Assignment)
|
(Divide
and Assignment)
|
(Modules
and Assignment)
|
|
Assigns
values from the right side operand to the left side operand
|
It
adds the right operand to the left operand and assigns the result to the left
operand.
|
It
subtracts the right operand from the left operand and assigns the result to
the left operand.
|
It
multiplies the right operand with the left operand and assigns the result to
the left operand.
|
It
divides the left operand with the right operand and assigns the result to the
left operand.
|
It
takes modulus using two operands and assigns the result to the left operand.
|
|
Example:-
R
= val1 + val2 will assign the value of val1+ val2 into R
|
Example:
-
R
+= val1 is equivalent to R = R + val1
|
Example:
-
R
-= val1 is equivalent to R= R – val1
|
Example:-
R*=
val1 is equivalent to R = R * val1
|
Example:-
R
/= val1 is equivalent to R = R / val1
|
Example
:-
R
%= val1 is equivalent to R= R % val1
|
String Operators
|
+ (String
addition)
|
+=
|
|
Use
to Concatenates two strings.
|
Used
to Concatenates two strings and assigns the result to the first operand.
|
Increment operators
|
Increment (++)
|
|
It
Adds one to its operand. If used as a prefix operator (++a), returns
the value of its operand after adding one; if used as a postfix operator
(a++), returns the value of its operand before adding one.
Example
:-
If
a=5;
Then
++a sets a to 6 whereas a++ return 5 and only then increase 1 in
the value of a (means it sets a to 6)
|
Comparison Operators
|
==
|
!=
|
>
|
>=
|
<
|
<=
|
|
It
will returns true if the operands are equal.
|
It
will returns true if the operands are not equal.
|
It
will returns true if left operand is greater than right operand.
|
It
will returns true if left operand is greater than or equal to right operand.
|
It
will returns true if left operand is less than right operand.
|
It
will returns true if left operand is less than or equal to right operand.
|
Special Operators
|
?:
|
It
will allow to perform a simple "if...then...else"
|
|
,
|
It
Evaluates two expressions and returns the result of the second expression.
|
|
delete
|
It
allow to delete an object property or an element at a specified
index in an array.
|
|
new
|
It
allow to create an instance of a user-defined object type or of one of
the built-in object types.
|
|
this
|
‘this’
is a Keyword that you can use to refer to the current object.
|
|
typeof
|
It
Returns a string indicating the type of the unevaluated operand.
|
|
void
|
void
operator specifies an expression to be evaluated without returning a value.
|
Logical Operators.
|
&& (Logical AND)
|
|| (Logical OR)
|
! (Logical NOT)
|
|
If
both the operands are non-zero, then the condition becomes true.
|
If
any of the two operands are non-zero, then the condition becomes true.
|
It
Reverses the logical state of its operand. If a condition is true, then the
Logical NOT operator will make it false.
|
|
Example
:-
(X
&& Y) is true.
|
Example
:-
(X||
Y) is true.
|
Example
:-
!
(X && Y) is false.
|
Bitwise operators
There are
SIX bitwise operators:
|
1. &
2. |
3. ^
4. ~
5. >>
6. <<
|
These
operators convert their operands to integers (truncating any floating point
towards 0), and perform the specified bitwise operation on them.