Types of Operators
- Assignment operators
- Comparison operators
- Arithmetic operators
- Bitwise operators
- Logical operators
- String operators
- Conditional (ternary) operator
- Comma operator
- Unary operators
- Relational operators
4 + 2 X * Y
A unary operator requires a single operand, either before or after the operator:
|Addition assignment |
|Subtraction assignment |
|Multiplication assignment |
|Division assignment |
|Remainder assignment |
|Left shift assignment |
|Right shift assignment |
|Unsigned right shift assignment |
|Bitwise AND assignment |
|Bitwise XOR assignment |
|Bitwise OR assignment |
|Exponentiation assignment |
|Logical AND assignment |
|Logical OR assignment |
|Logical nullish assignment |
Comparison operators are used in logical statements to determine equality or difference between variables or values.
|Operator||Description||Examples returning true|
|Equal (||Returns |
|Not equal (||Returns |
|Strict equal (||Returns |
|Strict not equal (||Returns |
|Greater than (||Returns |
|Greater than or equal (||Returns |
|Less than (||Returns |
|Less than or equal (||Returns |
Arithmetic operators perform arithmetic on numbers (literals or variables).
let x = 100 + 50; let x = a + b;
Bit operators work on 32 bits numbers.
|Bitwise AND||Returns a one in each bit position for which the corresponding bits of both operands are ones.|
|Bitwise OR||Returns a zero in each bit position for which the corresponding bits of both operands are zeros.|
|Bitwise XOR||Returns a zero in each bit position for which the corresponding bits are the same. [Returns a one in each bit position for which the corresponding bits are different.]|
|Bitwise NOT||Inverts the bits of its operand.|
|Left shift||Shifts |
|Sign-propagating right shift||Shifts |
|Zero-fill right shift||Shifts |
|&||AND||5 & 1||0101 & 0001||0001||1|
||||OR||5 | 1||0101 | 0001||0101||5|
|^||XOR||5 ^ 1||0101 ^ 0001||0100||4|
|<<||left shift||5 << 1||0101 << 1||1010||10|
|>>||right shift||5 >> 1||0101 >> 1||0010||2|
|>>>||unsigned right shift||5 >>> 1||0101 >>> 1||0010||2|
Logical operators are used to determine the logic between variables or values.
Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the
|| operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.
In addition to the comparison operators, which can be used on string values, the concatenation operator
(+) concatenates two string values together, returning another string that is the union of the two operand strings.
console.log('my ' + 'string'); // console logs the string "my string".
The shorthand assignment operator
+= can also be used to concatenate strings.
var mystring = 'alpha'; mystring += 'bet'; // evaluates to "alphabet" and assigns this value to mystring.
variablename = (condition) ? value1:value2
let voteable = (age < 18) ? "Too young" : "Old enough";
If the variable age is a value below 18, the value of the variable voteable will be “Too young”, otherwise the value of voteable will be “Old enough”.
Unary operators work on one value.
|+x||Unary Plus||Convert a value into a number|
|-x||Unary Minus||Convert a value into a number and negate it|
|++x||Increment Operator (Prefix)||Add one to the value|
|–x||Decrement Operator (Prefix)||Subtract one from the value|
|x++||Increment Operator (Postfix)||Add one to the value|
|x–||Decrement Operator (Postfix)||Subtract one from the value|
I hope you like this article.
Also Read :