Assignment operators assign values to variables. JavaScript supports several compound assignment operators that combine assignment with arithmetic, bitwise, logical, and shift operations. They help make code shorter and easier to read when updating an existing variable.
=) sets an initial value to a variable: x = y.+=, -=, *=, /=, %=, **= update a variable using arithmetic and reassign the result.<<=, >>=, >>>= shift bits of a number to the left or right.&=, ^=, |= apply bitwise logic on numbers and store the result back.&&=, ||=, ??= combine short-circuit logic with assignment, useful for setting default or fallback values.All compound assignment operators follow this basic pattern:
x op= y is equivalent to x = x op y
x += y → x = x + y, x *= y → x = x * y, etc.x <<= y → shift bits of x left by y positions, filling with zeros.x &= y → bitwise AND, x ^= y → bitwise XOR, x |= y → bitwise OR.&&=): assigns y to x only if x is truthy.||=): assigns y to x only if x is falsy.??=): assigns y to x only if x is null or undefined.| Operator | Example | Same As |
|---|---|---|
| = | x = y | x = y |
| += | x += y | x = x + y |
| -= | x -= y | x = x - y |
| *= | x *= y | x = x * y |
| /= | x /= y | x = x / y |
| %= | x %= y | x = x % y |
| **= | x **= y | x = x ** y |
let a = 10;
a += 5; // a = 15
a -= 3; // a = 12
a *= 2; // a = 24
a /= 4; // a = 6
a %= 4; // a = 2
a **= 3; // a = 8
| Operator | Example | Same As |
|---|---|---|
| <<= | x <<= y | x = x << y |
| >>= | x >>= y | x = x >> y |
| >>>= | x >>>= y | x = x >>> y |
let b = 4;
b <<= 1; // b = 8
let c = 16;
c >>= 2; // c = 4
let d = -8;
d >>>= 1; // d = 2147483644 (unsigned right shift)
| Operator | Example | Same As |
|---|---|---|
| &= | x &= y | x = x & y |
| ^= | x ^= y | x = x ^ y |
| |= | x |= y | x = x | y |
let e = 5; // 0101
e &= 3; // 0101 & 0011 = 0001 → e = 1
let f = 6; // 0110
f ^= 2; // 0110 ^ 0010 = 0100 → f = 4
let g = 1; // 0001
g |= 4; // 0001 | 0100 = 0101 → g = 5
| Operator | Example | Same As |
|---|---|---|
| &&= | x &&= y | x = x && (x = y) |
| ||= | x ||= y | x = x || (x = y) |
| ??= | x ??= y | x = x ?? (x = y) |
let h = true;
h &&= false; // h = false
let i = null;
i ||= "default"; // i = "default"
let j = undefined;
j ??= 100; // j = 100
let k = 0;
k ||= 42; // k = 42 (because 0 is falsy)
let m = 0;
m ??= 10; // m = 0 (nullish doesn't overwrite 0)
a is updated step by step using different operators, ending with a = 8.b = 4 becomes 8 after b <<= 1 (equivalent to multiplying by 2).0101 and 0011.||= replaces falsy values (like 0, "", null, undefined).??= only replaces null and undefined, keeping valid falsy values like 0.x = x + 1 → x += 1.??= only replaces null or undefined, not falsy values like 0 or ''.x and use x +=, x -=, x *=, and x /= to track how its value changes in the console.x %= y to keep a counter within a fixed range (like a circular index).x &&= y and x ||= y using different truthy and falsy values (0, "", null, undefined, "hello").??= with variables set to 0, "", null, and undefined to see which ones get replaced.<<= and >>= on small integers and compare the result with normal multiplication or division by powers of 2.Click the button to see count += 1 in action:
Current count: 0