Assignment operators in JavaScript are used to assign values to variables. These operators not only set the value of a variable but can also perform operations and assign the result to the variable simultaneously. This guide will cover the basic and compound assignment operators with examples to help you understand their usage.
1. Assignment Operators Overview:
Assignment operators assign values to variables. The basic assignment operator is =, but there are several compound assignment operators that combine assignment with an arithmetic operation.
| Operator | Description | Example | Equivalent to | Result (if x = 10 initially) |
|---|---|---|---|---|
= | Assignment | x = 10 | x = 10 | 10 |
+= | Addition Assignment | x += 5 | x = x + 5 | 15 |
-= | Subtraction Assignment | x -= 5 | x = x - 5 | 5 |
*= | Multiplication Assignment | x *= 5 | x = x * 5 | 50 |
/= | Division Assignment | x /= 5 | x = x / 5 | 2 |
%= | Modulus Assignment | x %= 5 | x = x % 5 | 0 |
**= | Exponentiation Assignment | x **= 2 | x = x ** 2 | 100 |
2. Basic Assignment (=):
The basic assignment operator = assigns the value on the right to the variable on the left.
Example:
let x = 10;
let y = 5;
y = x; // y is now 10
console.log(y); // Outputs: 10
3. Addition Assignment (+=):
The addition assignment operator += adds the value on the right to the variable on the left and assigns the result to the variable.
Example:
let x = 10;
x += 5; // x is now 15
console.log(x); // Outputs: 15
4. Subtraction Assignment (-=):
The subtraction assignment operator -= subtracts the value on the right from the variable on the left and assigns the result to the variable.
Example:
let x = 10;
x -= 5; // x is now 5
console.log(x); // Outputs: 5
5. Multiplication Assignment (*=):
The multiplication assignment operator *= multiplies the variable on the left by the value on the right and assigns the result to the variable.
Example:
let x = 10;
x *= 5; // x is now 50
console.log(x); // Outputs: 50
6. Division Assignment (/=):
The division assignment operator /= divides the variable on the left by the value on the right and assigns the result to the variable.
Example:
let x = 10;
x /= 5; // x is now 2
console.log(x); // Outputs: 2
7. Modulus Assignment (%=):
The modulus assignment operator %= divides the variable on the left by the value on the right and assigns the remainder to the variable.
Example:
let x = 10;
x %= 5; // x is now 0
console.log(x); // Outputs: 0
8. Exponentiation Assignment (=)**:
The exponentiation assignment operator **= raises the variable on the left to the power of the value on the right and assigns the result to the variable.
Example:
let x = 10;
x **= 2; // x is now 100
console.log(x); // Outputs: 100
9. Combined Usage:
You can combine multiple assignment operators in complex expressions to perform a series of operations.
Example:
let x = 10;
x += 5; // x is now 15
x *= 2; // x is now 30
x /= 3; // x is now 10
x -= 1; // x is now 9
console.log(x); // Outputs: 9
Conclusion:
Assignment operators are fundamental in JavaScript for assigning values to variables and performing compound operations. By mastering these operators, you can write more concise and efficient code. Whether you are adding, subtracting, multiplying, dividing, or performing more complex operations, assignment operators help you manage and manipulate variable values with ease.