In JavaScript, operators are symbols that perform specific actions on values. Operators are crucial for performing computations, comparisons, and logical operations in JavaScript.
Types of Operators in JavaScript
1. Arithmetic Operators
Here operators perform basic arithmetic operations.
Operator | Description | Example | Result | Explanation |
+ | Addition | 5 + 2 | 7 | Adds 5 and 2 together. |
- | Subtraction | 5 - 2 | 3 | Subtracts 2 from 5. |
* | Multiplication | 5 * 2 | 10 | Multiplies 5 by 2. |
/ | Division | 5 / 2 | 2.5 | Divides 5 by 2. The result is a floating-point number. |
% | Modulus (Remainder) | 5 % 2 | 1 | Divides 5 by 2 and returns the remainder. |
** | Exponentiation | 5 ** 2 | 25 | Raises 5 to the power of 2. |
++ | Increment (adds 1) | let a = 5; a++ | 6 (value of a ) | Increases a by 1. In postfix, the value before increment is used. |
-- | Decrement (subtracts 1) | let a = 5; a-- | 4 (value of a ) | Decreases a by 1. In postfix, the value before decrement is used. |
Notes:
Postfix increment/decrement (
a++
ora--
): Returns the old value first, then updates the variable.Prefix increment/decrement (
++a
or--a
): Updates the variable first, then returns the updated value.
2. Assignment Operators
These operators assign values to variables. Here’s the explanation presented in a table format:
Operator | Description | Example | Result (if a = 5 initially) | Explanation |
= | Assignment | a = 5 | a = 5 | Assigns the value 5 to the variable a . |
+= | Addition assignment | a += 3 | a = 8 | Adds 3 to a and updates a to 8 . |
-= | Subtraction assignment | a -= 2 | a = 3 | Subtracts 2 from a and updates a to 3 . |
*= | Multiplication assignment | a *= 2 | a = 10 | Multiplies a by 2 and updates a to 10 . |
/= | Division assignment | a /= 2 | a = 2.5 | Divides a by 2 and updates a to 2.5 . |
%= | Modulus assignment | a %= 2 | a = 1 | Finds the remainder when a is divided by 2 and updates a to 1 . |
**= | Exponentiation assignment | a **= 2 | a = 25 | Raises a to the power of 2 and updates a to 25 . |
Notes:
These operators combine an arithmetic operation with an assignment (
=
).They simplify the syntax by reducing
a = a + value
toa += value
.
3. Comparison Operators
These operators compare two values and return a boolean (true
or false
). Here’s a explanation in table format:
Operator | Description | Example | Result | Explanation |
== | Equality (loose comparison) | 5 == "5" | true | Compares the values of 5 and "5" after type conversion; they are equal. |
=== | Strict equality | 5 === "5" | false | Compares both value and type; 5 (number) is not equal to "5" (string). |
!= | Inequality (loose) | 5 != "5" | false | Compares the values after type conversion; they are equal, so it’s false . |
!== | Strict inequality | 5 !== "5" | true | Compares both value and type; they differ, so it’s true . |
> | Greater than | 5 > 2 | true | Checks if 5 is greater than 2 ; it is. |
< | Less than | 5 < 2 | false | Checks if 5 is less than 2 ; it is not. |
>= | Greater than or equal to | 5 >= 5 | true | Checks if 5 is greater than or equal to 5 ; it is. |
<= | Less than or equal to | 5 <= 4 | false | Checks if 5 is less than or equal to 4 ; it is not. |
Notes:
Loose comparison (
==
and!=
): Converts types before comparing values.Strict comparison (
===
and!==
): Requires both type and value to match.
4. Logical Operators
These operators are used to perform logical operations, mostly with boolean values.
1. Logical AND (&&
)
Description: Returns
true
if both operands aretrue
. If either operand isfalse
, it returnsfalse
.Behavior:
If both operands are
true
, the result istrue
.- Example:
true && true
results intrue
.
- Example:
If one operand is
false
, the result isfalse
.- Example:
false && true
results infalse
.
- Example:
If both operands are
false
, the result isfalse
.- Example:
false && false
results infalse
.
- Example:
Short-Circuiting:
- If the first operand is
false
, the second operand is not evaluated because the result is already determined to befalse
.
- If the first operand is
2. Logical OR (||
)
Description: Returns
true
if any operand istrue
. Returnsfalse
only if both operands arefalse
.Behavior:
If both operands are
true
, the result istrue
.- Example:
true || true
results intrue
.
- Example:
If one operand is
true
, the result istrue
.- Example:
false || true
results intrue
.
- Example:
If both operands are
false
, the result isfalse
.- Example:
false || false
results infalse
.
- Example:
Short-Circuiting:
- If the first operand is
true
, the second operand is not evaluated because the result is already determined to betrue
.
- If the first operand is
3. Logical NOT (!
)
Description: Negates the value of the operand. If the operand is
true
, it becomesfalse
. If it isfalse
, it becomestrue
.Behavior:
If the operand is
true
, the result isfalse
.- Example:
!true
results infalse
.
- Example:
If the operand is
false
, the result istrue
.- Example:
!false
results intrue
.
- Example:
5. Bitwise Operators
1. AND (&
)
Description: Compares each bit of two numbers. The result is
1
if both bits are1
; otherwise, it is0
.Example:
5 & 1
Binary Calculation:
5: 0101 1: 0001 ----------------- Result: 0001 (1 in decimal)
Result:
1
2. OR (|
)
Description: Compares each bit of two numbers. The result is
1
if at least one bit is1
; otherwise, it is0
.Example:
5 | 1
Binary Calculation:
5: 0101 1: 0001 ----------------- Result: 0101 (5 in decimal)
Result:
5
3. XOR (^
)
Description: Compares each bit of two numbers. The result is
1
if the bits are different; otherwise, it is0
.Example:
javascriptCopy code5 ^ 1
Binary Calculation:
5: 0101 1: 0001 ----------------- Result: 0100 (4 in decimal)
Result:
4
4. NOT (~
)
Description: Inverts all the bits of a number.
Example:
~5
Binary Calculation:
5: 00000000 00000000 00000000 00000101 (32-bit representation) ~5: 11111111 11111111 11111111 11111010 Result: -6
Result:
-6
5. Left Shift (<<
)
Description: Shifts the bits of the number to the left by the specified number of positions. Zeroes are filled on the right.
Example:
5 << 1
Binary Calculation:
5: 0101 ----------------- Shifted: 1010 (10 in decimal)
Result:
10
Shortcut: This operation multiplies the number by
2^n
, wheren
is the number of positions the bits are shifted.Example:
5 << 1
: Shift the binary representation of5
(which is0101
) by 1 position to the left, giving1010
(which is10
in decimal).Effectively,
5 * 2^1 = 5 * 2 = 10
.
6. Right Shift (>>
)
Description: Shifts the bits of the number to the right by the specified number of positions. The sign bit is preserved.
Example:
5 >> 1
Binary Calculation:
5: 0101 ----------------- Shifted: 0010 (2 in decimal)
Result:
2
Shortcut: This operation divides the number by
2^n
(and floors the result), wheren
is the number of positions the bits are shifted.Example:
5 >> 1
: Shift the binary representation of5
(which is0101
) by 1 position to the right, giving0010
(which is2
in decimal).Effectively,
5 / 2^1 = 5 / 2 = 2
(rounded down).
7. Unsigned Right Shift (>>>
)
Description: Shifts the bits of the number to the right by the specified number of positions. Zeroes are filled on the left, and the sign bit is ignored.
Example:
-5 >>> 1
Binary Calculation:
-5: 11111111 11111111 11111111 11111011 (32-bit signed integer) Shifted: 01111111 11111111 11111111 11111101 (ignores sign bit) Result: 2147483645
Result:
2147483645
6. Ternary (Conditional) Operator
The ternary operator is a shorthand for if
-else
conditions.
Operator | Description | Example | Result |
? : | Shorthand for if -else | let result = (5 > 2) ? "yes" : "no"; | yes |
7. String Operators
The +
operator is overloaded for strings to concatenate them.
Operator | Description | Example | Result |
+ | Concatenation | "Hello" + " World" | "Hello World" |
8. Spread Operator (...
)
The spread operator allows you to spread elements of an iterable (like an array) into individual elements.
Operator | Description | Example | Result |
... | Expands iterable elements | [...['a', 'b', 'c']] | ['a', 'b', 'c'] |
Spread operator will be discussed soon…