JavaScript Tutorial #05 Operators
In this lesson we will learn about operators in JavaScript.
An operator is a special type of symbols that performing specific types of operations. Like as 5 + 6 * 10 = 65 here is + and * is operators and 5, 6 and 10 is operands.
In JavaScript supports following types of operators.
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- String Operators
- Conditional (ternary) Operators
Arithmetic Operators
JavaScript support following arithmetic operators
Operators | Name |
---|---|
+ | addition |
- | subtraction |
* | multiplication |
/ | division |
% | modulo (returns the remainder) |
++ | increment (can be prefix or postfix) |
-- | decrement (can be prefix or postfix) |
Example
Assignment Operators
Assignment operator is used to assign a value to a variable.
Operators | Name |
---|---|
= | assign |
+= | add and assign |
-+ | subtract and assign |
*= | multiply and assign |
/= | divide and assign |
%= | modulo and assign |
Example
Comparison Operators
A comparison operators compare the operands with comparison operators and return logical value based on comparison is true.
Operators | Name |
---|---|
== | equal |
!= | not equal |
> | greater than |
>= | greater than or equal to |
< | less than |
<= | less than or equal to |
=== | identical (equal and of same type) |
!== | not identical |
Example
Logical Operators
Logical operators used with boolean values. More than one relation can be logically joined using logical operators.
Operators | Name |
---|---|
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
Example
Bitwise Operators
JavaScript support following binary bitwise operators.
Operators | Name |
---|---|
& | AND |
| | OR |
^ | XOR |
~ | NOT |
<< | shift left (zero fill at right) |
>> | shift right |
In Bitwise opertators converts operands in 32 bits numbers and work with bit by bit. For example the number of 5 and 6 have the binary represatation of 0101
and 0110
.
Example
String Operators
Using string opertators concatenate the string.
Operators | Name |
---|---|
+ | concatenation |
+= | concatenate and assign |
Example
Conditional (ternary) Operators
The conditional operator creates an expression that evaluates as one of two expressions depending on a condition.
conditions ? value1 : value2
Example