JavaScript Operators Explained
JavaScript operators are symbols that perform operations on variables and values. They can be categorized into different types:
1. Arithmetic Operators 🧮
Used for basic mathematical operations.
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 6 * 2 | 12 |
/ | Division | 10 / 2 | 5 |
% | Modulus (Remainder) | 10 % 3 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
++ | Increment | let x = 5; x++ | 6 |
-- | Decrement | let y = 8; y-- | 7 |
2. Assignment Operators 📝
Used to assign values to variables.
Operator | Example | Equivalent To |
---|---|---|
= | x = 10 | x = 10 |
+= | x += 5 | x = x + 5 |
-= | x -= 2 | x = x - 2 |
*= | x *= 3 | x = x * 3 |
/= | x /= 2 | x = x / 2 |
%= | x %= 3 | x = x % 3 |
**= | x **= 2 | x = x ** 2 |
3. Comparison Operators 🔍
Used to compare two values and return true
or false
.
Operator | Description | Example | Result |
---|---|---|---|
== | Equal to | 5 == '5' | true |
=== | Strict equal (type & value) | 5 === '5' | false |
!= | Not equal | 10 != 5 | true |
!== | Strict not equal | 10 !== '10' | true |
> | Greater than | 10 > 5 | true |
< | Less than | 4 < 7 | true |
>= | Greater than or equal to | 10 >= 10 | true |
<= | Less than or equal to | 6 <= 3 | false |
4. Logical Operators 🤔
Used to combine multiple conditions.
Operator | Description | Example | Result |
---|---|---|---|
&& | AND (both must be true) | true && false | false |
` | ` | OR (at least one must be true) | |
! | NOT (reverses value) | !true | false |
5. Bitwise Operators 🖥️
Perform bitwise operations on numbers.
Operator | Description | Example (5 & 1) | Result |
---|---|---|---|
& | AND | 5 & 1 | 1 |
` | ` | OR | `5 |
^ | XOR | 5 ^ 1 | 4 |
~ | NOT | ~5 | -6 |
<< | Left Shift | 5 << 1 | 10 |
>> | Right Shift | 5 >> 1 | 2 |
6. Ternary Operator (Conditional Operator) 🎭
Shorter way to write an if-else
statement.
7. Type Operators 🏷️
Used to check the type of a variable.
Operator | Description | Example | Result |
---|---|---|---|
typeof | Returns variable type | typeof "Hello" | "string" |
instanceof | Checks if an object is an instance of a class | obj instanceof Object | true |
8. Spread and Rest Operators (...
) 📌
Spread (...
) – Expands elements
Rest (...
) – Collects remaining values into an array
Conclusion
JavaScript operators are fundamental for performing operations on values.
- ✅ Arithmetic (
+
,-
,*
,/
,%
,++
,--
) - ✅ Assignment (
=
,+=
,-=
,*=
,/=
,%=
) - ✅ Comparison (
==
,===
,!=
,!==
,>
,<
,>=
,<=
) - ✅ Logical (
&&
,||
,!
) - ✅ Ternary (
condition ? trueValue : falseValue
) - ✅ Spread & Rest (
...
)
Want an example of a specific operator in action?