JavaScript, operators are symbols or keywords used to
perform operations on one or more values. JavaScript has several types of
operators, which includes the following below.
Arithmetic Operators
Arithmetic Operators: These operators are used for
basic arithmetic operations such as addition, subtraction, multiplication, and
division. The addition operator + can also be used for string concatenation.
Examples of arithmetic operators include +, -, *, /.
Addition (+)
In this example above, I have used the addition operator is
used to add two or more numbers or to concatenate two or more strings.
Subtraction (-)
In the example above, I have used the subtraction operator is used to subtract one number from another.
Multiplication (*)
In the example above, I used the multiplication operator is
used to multiply two or more numbers.
Division (/)
In the example above, I have used the division operator is
used to divide one number by another.
Remainder/Modulus (%)
In this example above, I have used the remainder/modulus
operator is used to find the remainder of dividing one number by another.
Increment (++)
In this example above, I have used the increment operator is
used to increase the value of a variable by 1.
Decrement (--)
.In this example above, I have used the decrement operator is
used to decrease the value of a variable by 1.
Exponentiation (**)
In this example above, I have used the exponentiation
operator is used to raise a number to a power.
Assignment Operators
Assignment Operators: These operators are used to assign a value to a variable. For example, the assignment operator = assigns the value on the right to the variable on the left. Other examples of assignment operators include +=, -=, *=, /=.
The addition assignment operator (+=)
In this example above, I have used the addition
assignment operator to add the value on the right-hand side to the variable on
the left-hand side and assign the result to the variable on the left-hand side.
The subtraction assignment operator (-=)
In this example above, I have used the subtraction
assignment operator to subtract the value on the right-hand side from the
variable on the left-hand side and assign the result to the variable on the
left-hand side.
The multiplication assignment operator (*=)
In this example above, I have used the multiplication
assignment operator to multiply the variable on the left-hand side by the value
on the right-hand side and assign the result to the variable on the left-hand
side.
The division assignment operator (/=)
In this example above, I have used the division
assignment operator to divide the variable on the left-hand side by the value
on the right-hand side and assign the result to the variable on the left-hand
side.
The modulus assignment operator (%=)
In this example above, I have used the modulus assignment
operator to calculate the remainder of the division of the variable on the
left-hand side by the value on the right-hand side and assign the result to the
variable on the left-hand side.
The exponentiation assignment operator (**=)
In this example above, I have used the exponentiation
assignment operator to raise the variable on the left-hand side to the power of
the value on the right-hand side and assign the result to the variable on the
left-hand side. In this example, 2 raised to the power of 3 is 8.
Comparison Operators
Comparison Operators: These operators are used to
compare two values and return a Boolean value (true or false). Examples of
comparison operators include ==, !=, ===, !==, <, >, <=, >=.
The equality operator (==)
The equality operator compares the values of two operands
and returns true if they are equal, regardless of their data types.
The strict equality operator (===)
The strict equality operator compares the values and the
data types of two operands and returns true if they are equal. In this example,
x is a number, and y is a string, so they are not strictly equal.
The not equal operator (!=)
The not-equal operator compares the values of two
operands and returns true if they are not equal.
The strict, not equal operator (!==)
The strict, not equal operator compares the values and
the data types of two operands and returns true if they are not equal.
The greater than operator (>)
The greater than operator compares two operands and
returns true if the left operand is greater than the right operand.
The less-than operator (<)
The less-than operator compares two operands and returns
true if the left operand is less than the right operand.
The greater than or equal to the operator (>=)
The greater than or equal to operator compares two
operands and returns true if the left operand is greater than or equal to the
right operand.
The less than or equal to the operator (<=)
The less than or equal to operator compares two operands
and returns true if the left operand is less than or equal to the right
operand.
Logical Operators
Logical operators: These operators are used to
perform logical operations on Boolean values. Examples of logical operators
include && (AND), || (OR), ! (NOT).
Logical AND (&&)
In this example above, I have used the logical AND operator
(&&) to check if both a is greater than 4 and b is greater than 2.
Since both conditions are true, the value of c is true.
Logical OR (||)
In this example above, I have used the logical OR operator
(||) to check if either a is greater than 6 or b is less than 2. Since neither
condition is true, the value of c is false.
Logical NOT (!)
In this example above, I have used the logical NOT operator
(!) to check if a is not greater than 4. Since a is indeed greater than 4, the
value of b is false.
Combining logical operators
In this example above, I have used a combination of logical
operators to check if a is not greater than 4 OR b is less than 2. Since the
second condition is true, the value of c is true. Note that the logical NOT
operator (!) has higher precedence than the logical OR operator (||).
Therefore, the expression !(a > 4) is evaluated first before it is combined
with the second condition using the logical OR operator.
Bitwise Operators
Bitwise Operators: These operators are used to
perform bitwise operations on binary representations of numbers. Examples of
bitwise operators include & (AND), | (OR), ^ (XOR), ~ (NOT), << (left
shift), >> (right shift), >>> (zero-fill right shift).
Bitwise AND (&) Operator
In this example above, I have used the bitwise AND (&) operator to perform a bitwise AND operation between the binary representations of the numbers 5 and 3. The binary representation of 5 is 0101, and the binary representation of 3 is 0011. Performing a bitwise AND between these two numbers results in the binary number 0001, which is equal to 1 in decimal notation.
Bitwise OR (|) Operator
In this example above, I have used the bitwise OR (|) operator to perform a bitwise OR operation between the binary representations of the numbers 5 and 3. The binary representation of 5 is 0101, and the binary representation of 3 is 0011. Performing a bitwise OR between these two numbers results in the binary number 0111, which is equal to 7 in decimal notation.
Bitwise XOR (^) Operator
In this example above, I have used the bitwise XOR (^)
operator to perform a bitwise XOR operation between the binary representations
of the numbers 5 and 3. The binary representation of 5 is 0101, and the binary
representation of 3 is 0011. Performing a bitwise XOR between these two numbers
results in the binary number 0110, which is equal to 6 in decimal notation.
Bitwise NOT (~) Operator
In this example above, I have used the bitwise NOT (~) operator to perform a bitwise NOT operation on the binary representation of the number 5. The binary representation of 5 is 0101. Performing a bitwise NOT operation on this number results in the binary number 1010 in 2's complement notation. In decimal notation, this is equal to -6.
Bitwise Left Shift (<<) Operator
In this example above, I
have used the bitwise left shift (<<) operator to perform a
bitwise left shift operation on the binary representation of the number 5.
Shifting the binary representation of 5 two bits to the left results in the
binary number 010100, which is equal to 20 in decimal notation.
Bitwise Right Shift (>>) Operator
In this example above, I have used the bitwise right shift
(>>).
Bitwise Zero-Fill Right Shift
In this example above, I have used the bitwise zero-fill
right shift (>>>) operator to perform a bitwise zero-fill right shift
operation on the binary representation of the number -10. The binary
representation of -10 in 2's complement notation is
11111111111111111111111111110110. Shifting this binary number two bits to the
right with zero-fill results in the binary number
00111111111111111111111111111101. In decimal notation, this is equal to
1073741821.
Conditional (Ternary) Operator
In the example above, I have used the conditional operator
to set the value of the message variable based on the value of the age
variable. The syntax of the conditional operator is condition ? expression1:
expression2, where the condition is evaluated as either true or false, and if
it's true, expression1 is returned, otherwise expression2 is returned.
In this example above, the condition (age >= 18) is
evaluated as true since the age is 20 which is greater than or equal to 18.
Therefore, the expression "You are an adult" is returned and assigned
to the message variable. If the age was less than 18, the condition would have been
evaluated as false, and the expression "You are minor" would have
been assigned to the message variable instead.
Overall, the conditional operator is a shorthand way of writing
an if-else statement in one line and can be useful for concise conditional
expressions.
Comma Operator
Comma Operator: This operator is used to evaluate
multiple expressions, returning the result of the last expression. The syntax
is expression1, expression2, ... expression.
In the example above, I have used the comma operator to
evaluate three expressions within parentheses: x += 1, y += 2, and z += 3.
These expressions modify the values of the variables x, y, and z. The comma
operator evaluates each expression from left to right and returns the result of
the last expression, which in this case is z += 3. The value of the result is
therefore 6, which is the result of the last expression, and the values of x,
y, and z are 2, 4, and 6, respectively, after the three expressions are
evaluated.
Comments