DEV Community

Cover image for Mastering JavaScript Assignment Operators — The Complete Guide
Raj Aryan
Raj Aryan

Posted on

Mastering JavaScript Assignment Operators — The Complete Guide

When working with JavaScript, assignment operators are something you’ll use every single day — whether you’re doing simple math, updating values in loops, or managing conditional logic. But do you know all of them, including the lesser-known logical and bitwise assignment operators?

Let’s break them down in the simplest way possible.


📌 What Are Assignment Operators in JavaScript?

Assignment operators assign values to JavaScript variables.
They can do more than just = — they can add, subtract, multiply, shift bits, or even work with logical conditions before assigning.


1️⃣ Basic Assignment Operators

Operator Example Same As
= x = y Assigns y to x
+= 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 (power)

Example:

let x = 5;
x += 10; // x = 15
x *= 2;  // x = 30
Enter fullscreen mode Exit fullscreen mode

2️⃣ Shift Assignment Operators

These work on binary representations of numbers.

Operator Example Same As
<<= x <<= y x = x << y (Left shift)
>>= x >>= y x = x >> y (Right shift, signed)
>>>= x >>>= y x = x >>> y (Right shift, unsigned)

Example:

let a = 8;   // 1000 in binary
a <<= 1;     // 10000 in binary → 16
Enter fullscreen mode Exit fullscreen mode

3️⃣ Bitwise Assignment Operators

These perform bitwise operations and assign the result.

Operator Example Same As
&= x &= y x = x & y
` =` `x = y` `x = x y`
^= x ^= y x = x ^ y

Example:

let b = 5;   // 0101
b &= 3;      // 0101 & 0011 → 0001 → 1
Enter fullscreen mode Exit fullscreen mode

4️⃣ Logical Assignment Operators (ES2020+)

These combine logical checks with assignment.

Operator Example Meaning
&&= x &&= y Assign y if x is truthy
` =` `x = y` Assign y if x is falsy
??= x ??= y Assign y if x is null or undefined

Example:

let c = 0;
c ||= 10; // c = 10 (because 0 is falsy)

let d = null;
d ??= 5; // d = 5 (because d was null)

let e = true;
e &&= "Hello"; // e = "Hello" (because true is truthy)
Enter fullscreen mode Exit fullscreen mode

💡 Quick Tip

These operators make your code cleaner by avoiding repetitive expressions like:

if (!x) { x = y; }
Enter fullscreen mode Exit fullscreen mode

You can now just write:

x ||= y;
Enter fullscreen mode Exit fullscreen mode

📝 Exercise for You

What will be the output of the following code?

let x = 5;
x += 10;
console.log(x);
Enter fullscreen mode Exit fullscreen mode

Answer:
15 🎯


🚀 Final Thoughts

JavaScript assignment operators are powerful shortcuts.
Mastering them will make your code shorter, cleaner, and easier to read — especially when dealing with complex logic or repetitive updates.

If you found this useful, 💖 like, 🖊 comment, and 🔄 share it with other JavaScript learners!

Top comments (0)