← Back to Chapters

JavaScript Assignment Operators

? JavaScript Assignment Operators

? Quick Overview

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.

? Key Concepts

  • Simple assignment (=) sets an initial value to a variable: x = y.
  • Arithmetic assignment operators like +=, -=, *=, /=, %=, **= update a variable using arithmetic and reassign the result.
  • Shift assignment operators like <<=, >>=, >>>= shift bits of a number to the left or right.
  • Bitwise assignment operators like &=, ^=, |= apply bitwise logic on numbers and store the result back.
  • Logical assignment operators &&=, ||=, ??= combine short-circuit logic with assignment, useful for setting default or fallback values.

? Syntax and Theory

All compound assignment operators follow this basic pattern:

x op= y is equivalent to x = x op y

  • Arithmetic: x += yx = x + y, x *= yx = x * y, etc.
  • Shift: x <<= y → shift bits of x left by y positions, filling with zeros.
  • Bitwise: x &= y → bitwise AND, x ^= y → bitwise XOR, x |= y → bitwise OR.
  • Logical AND assignment (&&=): assigns y to x only if x is truthy.
  • Logical OR assignment (||=): assigns y to x only if x is falsy.
  • Nullish coalescing assignment (??=): assigns y to x only if x is null or undefined.

? Code Examples

➕ Arithmetic Assignment Operators

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
? View Code Example
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

? Shift Assignment Operators

Operator Example Same As
<<= x <<= y x = x << y
>>= x >>= y x = x >> y
>>>= x >>>= y x = x >>> y
? View Code Example
let b = 4;
b <<= 1;  // b = 8

let c = 16;
c >>= 2;  // c = 4

let d = -8;
d >>>= 1; // d = 2147483644 (unsigned right shift)

⚙️ Bitwise Assignment Operators

Operator Example Same As
&= x &= y x = x & y
^= x ^= y x = x ^ y
|= x |= y x = x | y
? View Code Example
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

? Logical Assignment Operators

Operator Example Same As
&&= x &&= y x = x && (x = y)
||= x ||= y x = x || (x = y)
??= x ??= y x = x ?? (x = y)
? View Code Example
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)

? Output and Explanation

  • In the arithmetic example, a is updated step by step using different operators, ending with a = 8.
  • Shift operators move bits left or right. For example, b = 4 becomes 8 after b <<= 1 (equivalent to multiplying by 2).
  • Bitwise operators work on the binary representation of numbers, which is why comments show binary values like 0101 and 0011.
  • Logical assignment operators are great for defaults:
    • ||= replaces falsy values (like 0, "", null, undefined).
    • ??= only replaces null and undefined, keeping valid falsy values like 0.

? Tips and Best Practices

  • Use compound assignment to reduce repetitive code such as x = x + 1x += 1.
  • ??= only replaces null or undefined, not falsy values like 0 or ''.
  • Use logical assignment to safely set default values for configuration objects, function parameters, or user input.
  • Prefer readability: avoid chaining many compound assignments in a single line.
  • Be careful with bitwise and shift operators, especially on negative numbers, because results can be surprising.

? Try It Yourself

  • Declare a variable x and use x +=, x -=, x *=, and x /= to track how its value changes in the console.
  • Write a loop that uses x %= y to keep a counter within a fixed range (like a circular index).
  • Experiment with x &&= y and x ||= y using different truthy and falsy values (0, "", null, undefined, "hello").
  • Try ??= with variables set to 0, "", null, and undefined to see which ones get replaced.
  • Use <<= and >>= on small integers and compare the result with normal multiplication or division by powers of 2.

? Mini Interactive Demo

Click the button to see count += 1 in action:

Current count: 0