|
JavaScript : Operator
An operator is a symbol or sign used in javascript to identify a specific operation.
In an experation, a + b = c; a, b and c are operands while + and = are operators.
Types Of Operators in Javascript
There are three type of operators based on operands used, namely unary, binary, ternary.
Unary The Operator works with one operand (e.g: a++, a will be incremented by 1),
Binary The Operator works with two operands (e.g: a+b),
Ternary The Operator works with three operands (e.g: condition? value 1 : value 2).
There a four type of operators based on the type of use.
Arithmetic Operators They are operators or syntax used to do arithmetic or
math operations like addition, subtraction, multiplication, division, increment,
decrement, etc.
| Operator |
Description |
| + |
Used for addition of two numbers |
| - |
Used for subtraction of two numbers |
| * |
Used for multiplication of two numbers |
| / |
Used for division of two numbers |
| % |
Used to find the division remainder |
| ++ |
Used to increment a value |
| -- |
Used to decrement a value |
Logical Operators They are used mainly for boolean operations.
| Operator |
Description |
| && |
Used for logical "and" operation, condition |
| || |
Used for logical "or" operation, condition |
| ! |
Used for logical "not" operation, condition |
Local operators or used along with if statements and while loops to check multiple criterias.
Comparison Operators They are used to compare numerical (numbers), string or boolean values
| Operator |
Description |
| == |
validates the condition whether two numbers or string values are equal |
| != |
validates the condition whether two numbers or string values are not equal |
| > |
validates the condition whether one numbers is greater than other |
| < |
validates the condition whether one numbers is less than other |
| >= |
compares the numbers and checks whether one numbers is greater than or equal to other |
| <= |
compares the numbers and checks whether one numbers is less than or equals other |
| === |
used to compare and check whether two values are strictly equal |
| !== |
used to compare and check whether two values are strictly not equal |
The main difference between "equal to (==)" and "strictly equal to (===)"
is that the equality comparison takes place after type conversion for "equal to (==)"
and before type conversion for "strictly equal to (===)".
i.e "5" == 5
and "5" !== 5
Assignment Operators Assignment Operators are use to assign a value to a variable.
| Operator |
Description |
| = |
used to assign a value on the right side to the variable of the left side of the operator. |
| += |
it adds the value on the right to the previous value of the variable on the left and assign the new value to the variable. |
| -= |
it subtracts the value on the right to the previous value of the variable on the left and assign the new value to the variable. |
| *= |
it multiplies the operand on the right to the previous value of the variable on the left and assaigns the new value to the variable. |
| /= |
it divides the operand on the right to the previous value of the variable on the left and assaigns the new value to the variable. |
| >>= |
The operand on the left is shifted left by the value on the right. |
| <<= |
A signed right shifted is performed on the left operand by the value on the right. |
Ternary Operator As the name indicates ternary operators take three operands. The syntax is condition ? result1 : result2;.
Here you use a condition before question mark (?) followed by result 1 and result 2 separated by a colon (:).
Result1 is called if the condition is satisfied else result2 is called.
|