Course contentsShow
← JavaScript
Lesson 250 of 2,7237. Arithmetic and OperatorsFree lesson

Bitwise Assignment Operators

Apply bitwise operations with assignment using &=, |=, ^=, <<=, >>=, and >>>= operators.

Bitwise Assignment Operators

What you'll learn: How to combine bitwise operations with assignment to modify variables in a single step.

Just like how += combines addition with assignment, JavaScript provides compound operators that perform bitwise operations and assign the result back to the variable—all in one go.

The Bitwise Assignment Family

These operators work with binary representations of numbers:

  • &= (AND assignment): Keeps only bits that are 1 in both operands
  • |= (OR assignment): Sets bits that are 1 in either operand
  • ^= (XOR assignment): Toggles bits that differ between operands
  • <<= (left shift assignment): Shifts bits left, filling with zeros
  • >>= (right shift assignment): Shifts bits right, preserving sign
  • >>>= (unsigned right shift assignment): Shifts bits right, filling with zeros

How They Work

Instead of writing x = x & 5, you can write x &= 5. They're equivalent, just like x += 3 means x = x + 3.

let flags = 12;  // Binary: 1100
flags &= 10;     // Binary: 1010
// Result: 8 (binary: 1000)
// Same as: flags = flags & 10

let permissions = 4;
permissions |= 2;  // Add a permission flag
// Result: 6 (combines bits)

These operators are rarely needed in everyday JavaScript, but they're powerful for working with permission systems, color values, or performance-critical binary manipulations.

Key Takeaway: Bitwise assignment operators (&=, |=, ^=, <<=, >>=, >>>=) combine bitwise operations with assignment, modifying a variable's binary representation in place—useful for flags, masks, and low-level data manipulation.