Spex3
    

JavaScript Assignment Operators 📝


    

    

JavaScript Assignment Operators 📝
Assignment operators assign values to variables. They can also modify values before assigning them.

1️⃣ List of Assignment Operators
Operator Example Equivalent To Description
= x = 5 x = 5 Assigns 5 to x.
+= x += 2 x = x + 2 Adds and assigns.
-= x -= 3 x = x - 3 Subtracts and assigns.
*= x *= 4 x = x * 4 Multiplies and assigns.
/= x /= 2 x = x / 2 Divides and assigns.
%= x %= 3 x = x % 3 Finds remainder and assigns.
**= x **= 2 x = x ** 2 Exponentiates and assigns.

2️⃣ Examples of Assignment Operators
✅ 1. Simple Assignment (=)
Assigns a value to a variable.


let a = 10;
console.log(a); // 10

✅ 2. Addition Assignment (+=)
Adds a value to the variable and assigns the result.


let b = 5;
b += 3; // Same as: b = b + 3
console.log(b); // 8

✅ 3. Subtraction Assignment (-=)
Subtracts a value from the variable and assigns the result.


let c = 10;
c -= 4; // Same as: c = c - 4
console.log(c); // 6


✅ 4. Multiplication Assignment (*=)
Multiplies the variable by a value and assigns the result.


let d = 7;
d *= 2; // Same as: d = d * 2
console.log(d); // 14


✅ 5. Division Assignment (/=)
Divides the variable by a value and assigns the result.


let e = 20;
e /= 5; // Same as: e = e / 5
console.log(e); // 4


✅ 6. Modulus Assignment (%=)
Divides the variable by a value, assigns the remainder.


let f = 11;
f %= 4; // Same as: f = f % 4
console.log(f); // 3


✅ 7. Exponentiation Assignment (**=)
Raises the variable to a power and assigns the result.


let g = 3;
g **= 3; // Same as: g = g ** 3
console.log(g); // 27


3️⃣ Best Practices
✅ Use +=, -=, *=, etc., for concise code.
✅ Use **= for exponentiation instead of Math.pow().
✅ Use %= to check even/odd numbers or find remainders.


    Date: 2025-03-29 00:00:00.000000