Assignment Operators in JavaScript
💡 What Are Assignment Operators?
Assignment operators in JavaScript are used to assign values to variables. The most common one is the = operator, but JavaScript also provides compound assignment operators to combine arithmetic or bitwise operations with assignment.
>>“Assignment is how values settle into variables — it’s how JavaScript remembers.” 🧠
📌 Basic Assignment
Basic Assignment
let a = 10; // assigns the value 10 to variable a🧮 Compound Assignment Operators
Compound assignment operators simplify code by combining arithmetic and assignment into a single operator.
| Operator | Example | Equivalent To |
|---|---|---|
| += | a += b | a = a + b |
| -= | a -= b | a = a - b |
| *= | a *= b | a = a * b |
| /= | a /= b | a = a / b |
| %= | a %= b | a = a % b |
| **= | a **= b | a = a ** b |
Note
💡 Compound operators are useful for writing concise code, especially in loops and calculations.
📊 Example
Compound Assignment in Action
let score = 50;
score += 10; // score = 60
score -= 5; // score = 55
score *= 2; // score = 110
score /= 10; // score = 11
score %= 4; // score = 3
score **= 2; // score = 9🧪 Bitwise Assignment Operators
JavaScript also includes bitwise versions of assignment operators:
| Operator | Meaning |
|---|---|
| &= | Bitwise AND and assign |
| |= | Bitwise OR and assign |
| ^= | Bitwise XOR and assign |
| <<= | Left shift and assign |
| >>= | Right shift and assign |
| >>>= | Unsigned right shift and assign |
Note
⚠️ Bitwise operators are more advanced and used in low-level tasks like graphics, cryptography, or performance tuning.
🧠 Summary
- = assigns a value
- Compound operators (+=, -=, *=, /=, etc.) combine math and assignment
- Bitwise assignment operators modify variables using binary operations
🔗 Resources
>>“A well-assigned variable is the first step to clean logic.” ✍️