Assignment operators in JavaScript allow us to assign values to variables

The most common assignment operator is the `=`

operator. This operator is used to assign the value on the right to the variable on the left.

In addition to the basic assignment operator (`=`

), there are compound assignment operators that combine an arithmetic operation with the assignment, simplifying and optimizing the code.

If you want to learn more about Assignment Operators

check out the **Introduction to Programming Course** read more

## Basic assignment operator (`=`

)

The most common assignment operator is the `=`

operator, which is used to assign the value on the right side to the variable on the left side.

Operator | Name | Description |
---|---|---|

`=` | Assignment | Assigns a value to a variable |

Let’s see it with an example,

`let a = 5; // Assigns the value 5 to the variable a`

It can also be used to update the value of an existing variable:

```
let a = 5; // Initial assignment
a = 10; // Update the value
```

Do not confuse the assignment operator `=`

with the equality operator `==`

.

## Compound assignment operators

In addition to the basic operator, JavaScript offers several compound assignment operators that combine arithmetic operations with assignment. These operators allow you to write more compact code.

Operator | Name | Description |
---|---|---|

`+=` | Add and assign | Adds the value to the variable and assigns the result |

`-=` | Subtract and assign | Subtracts the value from the variable and assigns the result |

`*=` | Multiply and assign | Multiplies the value of the variable and assigns the result |

`/=` | Divide and assign | Divides the value of the variable and assigns the result |

`%=` | Modulo and assign | Calculates the modulo of the value of the variable and assigns the result |

### Add and Assign (`+=`

)

The `+=`

operator adds a value to a variable and assigns the result to that variable.

```
let a = 5;
a += 3; // a is now 8 (equivalent to a = a + 3)
```

### Subtract and Assign (`-=`

)

The `-=`

operator subtracts a value from a variable and assigns the result to that variable.

```
let a = 10;
a -= 3; // a is now 7 (equivalent to a = a - 3)
```

### Multiply and Assign (`*=`

)

The `*=`

operator multiplies a variable by a value and assigns the result to that variable.

```
let a = 4;
a *= 6; // a is now 24 (equivalent to a = a * 6)
```

### Divide and Assign (`/=`

)

The `/=`

operator divides a variable by a value and assigns the result to that variable.

```
let a = 20;
a /= 4; // a is now 5 (equivalent to a = a / 4)
```

### Modulo and Assign (`%=`

)

The `%=`

operator calculates the modulo of a variable by a value and assigns the result to that variable.

```
let a = 17;
a %= 5; // a is now 2 (equivalent to a = a % 5)
```

### Exponentiation and Assign (`**=`

)

The `**=`

operator raises a variable to the power of a value and assigns the result to that variable.

```
let a = 2;
a **= 3; // a is now 8 (equivalent to a = a ** 3)
```

## Operator precedence

It is important to note that assignment operators have a lower precedence than most other operators in JavaScript. Therefore, expressions on the right side of assignment operators are evaluated first.

```
let a = 5;
let b = 10;
a += b * 2; // a is now 25 (b * 2 is evaluated first, then a += 20)
```