Last modified: Jul 20, 2026
JS Variable Assignment Operators Guide
JavaScript variable assignment operators are essential tools for storing and updating data in your code. They allow you to assign values to variables and modify them efficiently. Understanding these operators helps you write cleaner, more readable, and less error-prone code. This guide covers all common assignment operators, from the basic equals sign to compound operators like += and **=. Each operator is explained with simple examples and practical use cases.
What Are Assignment Operators?
Assignment operators are symbols that assign a value to a variable. The most basic one is the equals sign (=). It sets the variable on the left to the value on the right. Compound assignment operators combine an arithmetic or bitwise operation with assignment. They perform the operation and then assign the result in one step.
For example, x += 5 is shorthand for x = x + 5. These operators make your code shorter and often easier to read. They are widely used in loops, counters, and data transformations.
The Basic Assignment Operator (=)
The = operator assigns the right-hand value to the left-hand variable. It does not test equality. That is the role of == or ===. Always use = for assignment only.
// Basic assignment
let age = 25;
let name = "Alice";
let isActive = true;
console.log(age); // Output: 25
console.log(name); // Output: Alice
console.log(isActive); // Output: true
Addition Assignment (+=)
The += operator adds the right operand to the left variable and assigns the result. It works with numbers and strings. For strings, it concatenates them.
let score = 10;
score += 5; // Equivalent to: score = score + 5
console.log(score); // Output: 15
let greeting = "Hello";
greeting += " World"; // Equivalent to: greeting = greeting + " World"
console.log(greeting); // Output: Hello World
Subtraction Assignment (-=)
The -= operator subtracts the right operand from the left variable and assigns the result. It works only with numbers.
let balance = 100;
balance -= 20; // Equivalent to: balance = balance - 20
console.log(balance); // Output: 80
Multiplication Assignment (*=)
The *= operator multiplies the left variable by the right operand and assigns the result. It works with numbers.
let width = 5;
width *= 3; // Equivalent to: width = width * 3
console.log(width); // Output: 15
Division Assignment (/=)
The /= operator divides the left variable by the right operand and assigns the result. It works with numbers and can produce decimal values.
let total = 10;
total /= 4; // Equivalent to: total = total / 4
console.log(total); // Output: 2.5
Remainder Assignment (%=)
The %= operator calculates the remainder of dividing the left variable by the right operand and assigns the result. It is useful for checking even/odd numbers or cycling through values.
let number = 17;
number %= 5; // Equivalent to: number = number % 5
console.log(number); // Output: 2
Exponentiation Assignment (**=)
The **= operator raises the left variable to the power of the right operand and assigns the result. It is available in ES2016 and later.
let base = 2;
base **= 3; // Equivalent to: base = base ** 3
console.log(base); // Output: 8
Bitwise Assignment Operators
Bitwise operators work on the binary representation of numbers. They are less common but useful in low-level programming or performance-critical code. The common ones are &= (AND), |= (OR), ^= (XOR), <<= (left shift), >>= (right shift), and >>>= (unsigned right shift).
let flags = 5; // Binary: 0101
flags &= 3; // Binary: 0011 -> Result: 0001 (1)
console.log(flags); // Output: 1
let value = 8; // Binary: 1000
value >>= 2; // Shift right by 2 -> Binary: 0010 (2)
console.log(value); // Output: 2
Logical Assignment Operators (ES2021)
Logical assignment operators combine logical operations with assignment. They are &&=, ||=, and ??=. They assign a value only if a condition is met.
Logical AND assignment (&&=) assigns the right value only if the left value is truthy. Logical OR assignment (||=) assigns the right value only if the left value is falsy. Nullish coalescing assignment (??=) assigns the right value only if the left value is null or undefined.
let user = { name: "Bob" };
user.name &&= "Alice"; // Assigns "Alice" because user.name is truthy
console.log(user.name); // Output: Alice
let config = { timeout: 0 };
config.timeout ||= 1000; // Does not assign because 0 is falsy
console.log(config.timeout); // Output: 0 (falsy)
let settings = { theme: null };
settings.theme ??= "dark"; // Assigns "dark" because null
console.log(settings.theme); // Output: dark
Operator Precedence and Chaining
Assignment operators have low precedence. They are evaluated after most other operations. You can chain assignments, but it can hurt readability. For example, a = b = c = 5 assigns 5 to all three variables. However, it is often better to write each assignment on its own line for clarity.
Understanding variable scope is also important when using assignment operators. Variables declared with let or const inside a block are not accessible outside. For more on this, read our JavaScript Variable Scope Explained guide.
Common Mistakes and Best Practices
A common mistake is confusing = with == or ===. Always use = for assignment and === for strict equality. Another mistake is forgetting that compound operators modify the variable in place. Make sure the variable already exists before using a compound operator.
Use descriptive variable names to make your code self-documenting. For example, totalPrice += tax is clearer than x += y. Also, avoid using assignment inside conditions like if (x = 5). This is a common bug. Instead, use if (x === 5).
If you are new to JavaScript, check out our JavaScript Variable Declaration Guide to understand how to declare variables properly before using assignment operators.
Practical Examples
Here is a complete example that uses several assignment operators in a real-world scenario.
// Shopping cart example
let cartTotal = 0;
// Add items
cartTotal += 25.99; // Adds item price
cartTotal += 12.50; // Adds another item
console.log("Cart total after items:", cartTotal); // Output: 38.49
// Apply discount
cartTotal *= 0.9; // 10% discount
console.log("Cart total after discount:", cartTotal); // Output: 34.641
// Round to two decimals
cartTotal = Math.round(cartTotal * 100) / 100;
console.log("Final cart total:", cartTotal); // Output: 34.64
Conclusion
JavaScript variable assignment operators are powerful tools that simplify your code. The basic = operator assigns values. Compound operators like +=, -=, *=, and /= combine an operation with assignment. Bitwise and logical assignment operators offer more specialized functionality. Always use them correctly to avoid bugs. Practice with examples to build confidence. For more on naming your variables well, see our JavaScript Variable Naming Rules guide. Happy coding!