1. Basic Assignment Operator =
Stores the value of the right-hand expression into the left-hand variable, overwriting any pre-existing value.
Syntax:
variable = value_or_expression;
int age;
age = 10; // Assign the value 10 to age
String name;
name = "sam"; // String value assignmentCode language: JavaScript (javascript)
2. Null-Aware Assignment Operator ??=
Only assigns the value if the left-hand variable is null. If the variable holds a non-null value, its original content remains unchanged.
Syntax:
variable ??= value;
This shorthand is equivalent to writing:
if (variable == null) {
variable = value;
}Code language: JavaScript (javascript)
Example:
void main() {
int? total = null;
total ??= 5;
print(total); // Outputs 5 — assignment runs since total was null
int? count = 100;
count ??= 200;
print(count); // Outputs 100 — assignment skipped, count is non-null
}Code language: PHP (php)
3. Compound Assignment Operators
Combines an arithmetic/bitwise operation and assignment into one step
For any operator op:
a op= b is functionally identical to a = a op b
| Category | Operator | Description |
|---|---|---|
| Basic Arithmetic | += | Add then assign result |
-= | Subtract then assign result | |
*= | Multiply then assign result | |
/= | Float division then assign result | |
| Integer Division | ~/= | Truncating integer division then assign |
| Modulo | %= | Remainder division then assign |
| Bitwise Left Shift | <<= | Left bit shift then assign |
| Bitwise Right Shift | >>= | Signed right bit shift then assign |
| Unsigned Right Shift | >>>= | Unsigned right bit shift then assign |
| Bitwise AND | &= | Bitwise AND then assign |
| Bitwise XOR | ^= | Bitwise XOR then assign |
| Bitwise OR | |= | Bitwise OR then assign |
| General rule for compound operators: | a op= b | a = a op b |
| Sample translation: | a += b | a = a + b |
Practical examples:
void main() {
var num = 2;
// *= Multiplication compound assignment
num *= 3;
print(num); // 6, equivalent to num = num * 3
// += Addition
num += 4;
print(num); // 10, equivalent to num = num + 4
// -= Subtraction
num -= 2;
print(num); // 8
// /= Floating-point division
double d = 10;
d /= 2;
print(d); // 5.0
// ~/= Integer truncating division
int n = 7;
n ~/= 2;
print(n); // 3
// %= Modulo remainder
int m = 9;
m %= 2;
print(m); // 1
// <<= Left bit shift
int bit1 = 1;
bit1 <<= 2; // 1 << 2 = 4
print(bit1); // 4
// >>= Signed right bit shift
int bit2 = 8;
bit2 >>= 2; // 8 >> 2 = 2
print(bit2); // 2
// >>>= Unsigned right bit shift
int bit3 = -8;
bit3 >>>= 2;
print(bit3); //4611686018427387902, a very large positive number
// &= Bitwise AND
int x = 6; // Binary 110
x &= 3; // Binary 011
print(x); // Result binary 010 = decimal 2
// ^= Bitwise XOR
int y = 5;
y ^= 3;
print(y);
// |= Bitwise OR
int z = 4;
z |= 1;
print(z); // 5
}Code language: PHP (php)
Let’s break down the left shift example: int bit1 = 1; shifted left by 2 positions.
The 32-bit binary representation of decimal 1 is:
00000000 00000000 00000000 00000001
x << n: Shifts all bits of x left by n positions. Empty low-order bits on the right fill with 0, and overflow high-order bits on the left get discarded entirely.
After shifting left twice, the binary becomes:
00000000 00000000 00000000 00000100
This binary value translates to decimal 4.
We will cover logical operators in a later section. For now, focus on mastering these assignment operators — they are the core takeaway of this lesson.
void main() {
// 1. Basic assignment =
var a = 2;
print(a == 2);
// 2. Multiplication compound assignment *=
a *= 3; // Equivalent to a = a * 3;
print(a == 6);
// 3. Addition compound assignment +=
a += 4; // Equivalent to a = a + 4;
print(a == 10);
// 4. Null-aware assignment ??=
int? b; // Declared nullable int, defaults to null
b ??= 20;
print(b == 20);
b ??= 99; // b is no longer null; value remains unchanged
print(b == 20);
// 5. Subtraction compound assignment -=
a -= 5;
print(a == 5);
// 6. Modulo compound assignment %=
a %= 3;
print(a == 2);
}Code language: Dart (dart)
Every print statement above will output true