Assignment Operators
JavaScript Assignment
Assignment operators assign values to JavaScript variables.
Given that x = 10 and y = 5, the table below explains the assignment operators:
| Oper | Name / Link | Example | Same As | Result |
|---|---|---|---|---|
| = | Simple | x = y | x = y | x = 5 |
| += | Add | x += y | x = x + y | x = 15 |
| -= | Subtract | x -= y | x = x - y | x = 5 |
| *= | Multiply | x *= y | x = x * y | x = 50 |
| /= | Divide | x /= y | x = x / y | x = 2 |
| %= | Remainder | x %= y | x = x % y | x = 0 |
| : | Colon | x: 45 | x = 45 | x = 45 |
Logical Assignment Operators
| Oper | Name / Link | Example | Result |
|---|---|---|---|
| &&= | AND | true &&= 10 | x = 10 |
| ||= | OR | false ||= 10 | x = 10 |
| ??= | Nullish | null ??= 10 | x = 10 |
Notes
The ??= operator above is an Assignment Operator (nullish assignment).
It is closely related to the ?? Logical Operator (nullish coalescing).