## JavaScript Assignment Operators

Assignment operators assign values to JavaScript variables.

**=**: Assigns the value on the right to the variable on the left.- Example:
means`x = y`

is assigned the value of`x`

.`y`

- Example:
**+=**: Adds the right operand to the left operand and assigns the result to the left operand.- Example:
is the same as`x += y`

.`x = x + y`

- Example:
**-=**: Subtracts the right operand from the left operand and assigns the result to the left operand.- Example:
is the same as`x -= y`

.`x = x - y`

- Example:
***=**: Multiplies the left operand by the right operand and assigns the result to the left operand.- Example:
is the same as`x *= y`

.`x = x * y`

- Example:
**/=**: Divides the left operand by the right operand and assigns the result to the left operand.- Example:
is the same as`x /= y`

.`x = x / y`

- Example:
**%=**: Calculates the remainder of dividing the left operand by the right operand and assigns the result to the left operand.- Example:
is the same as`x %= y`

.`x = x % y`

- Example:
****=**: Raises the left operand to the power of the right operand and assigns the result to the left operand.- Example:
is the same as`x **= y`

`x = x ** y`

- Example:

## Shift Assignment Operators

JavaScript also includes bitwise shift assignment operators, which are used to shift the bits of the first operand, then assign the result back to that operand. Here's how each works:

**<<= (Left Shift Assignment)**: Shifts the bits of the variable on the left operand to the left by the number of positions specified by the right operand, then assigns the result back to the left operand.- Example:
is the same as`x <<= y`

.`x = x << y`

- Example:
**>>= (Right Shift Assignment)**: Shifts the bits of the variable on the left operand to the right by the number of positions specified by the right operand, preserving the sign, then assigns the result back to the left operand.- Example:
is the same as`x >>= y`

.`x = x >> y`

- Example:
**>>>= (Unsigned Right Shift Assignment)**: Shifts the bits of the variable on the left operand to the right by the number of positions specified by the right operand, filling the new positions with zeros, then assigns the result back to the left operand.- Example:
is the same as`x >>>= y`

.`x = x >>> y`

- Example:

## Bitwise Assignment Operators

**&= (Bitwise AND Assignment)**: Applies the bitwise AND operation to both operands and assigns the result back to the left operand.**Example**:is the same as`x &= y`

.`x = x & y`

**^= (Bitwise XOR Assignment)**: Applies the bitwise XOR operation to both operands and assigns the result back to the left operand.**Example**:is the same as`x ^= y`

.`x = x ^ y`

**|= (Bitwise OR Assignment)**: Applies the bitwise OR operation to both operands and assigns the result back to the left operand.**Example**:is the same as`x |= y`

.`x = x | y`

## Logical Assignment Operators

**&&= (Logical AND Assignment)**: Assigns the right operand to the left operand only if the left operand is truthy.**Example**:effectively means`x &&= y`

. It will assign`x = x && (x = y)`

to`y`

if`x`

is truthy; otherwise,`x`

retains its original value.`x`

**||= (Logical OR Assignment)**: Assigns the right operand to the left operand if the left operand is falsy.**Example**:translates to`x ||= y`

. It will assign`x = x || (x = y)`

to`y`

if`x`

is falsy; otherwise,`x`

retains its value.`x`

**??= (Nullish Coalescing Assignment)**: Assigns the right operand to the left operand only if the left operand isor`null`

.`undefined`

**Example**:means`x ??= y`

. This operator will assign`x = x ?? (x = y)`

to`y`

if`x`

is`x`

or`null`

; otherwise,`undefined`

remains unchanged.`x`

## The = Operator

The Simple Assignment Operator (** =**) in JavaScript is used to assign values directly to variables. This operator can assign both straightforward values and results of expressions.

**Simple Assignment Examples:**

assigns the value`let x = 10;`

directly to the variable`10`

.`x`

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Simple Assignment</h2>
<h3>The = Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
```

assigns the result of the expression`let x = 10 + y;`

to the variable`10 + y`

, where`x`

needs to be previously defined or declared`y`

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Simple Assignment</h2>
<h3>The = Operator</h3>
<p id="demo"></p>
<script>
let y = 50
let x = 10 + y;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
```

## The += Operator

The Addition Assignment Operator (** +=**) in JavaScript is used to add a value to a variable and then update the variable with the new sum. This operator simplifies adding a value to a variable and reassigning the result to the same variable in one step.

**Addition Assignment Examples:**

sets the initial value of`let x = 10;`

to 10.`x`

adds 5 to the current value of`x += 5;`

, resulting in`x`

becoming 15`x`

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Addition Assignment</h2>
<h3>The += Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x += 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
```

let text = "Hello"; text += " World";

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Addition Assignment</h2>
<h3>The += Operator</h3>
<p id="demo"></p>
<script>
let text = "Hello";
text += " World";
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
```

## The -= Operator

The Subtraction Assignment Operator (** -=**) in JavaScript is used to subtract a value from a variable and then automatically update the variable with the new result. This operator efficiently combines a subtraction and an assignment into one operation.

**Subtraction Assignment Example:**

initializes the variable`let x = 10;`

with the value 10.`x`

then subtracts 5 from the current value of`x -= 5;`

, resulting in`x`

becoming 5.`x`

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Subtraction Assignment</h2>
<h3>The -= Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x -= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
```

## The *= Operator

The Multiplication Assignment Operator (** *=**) in JavaScript multiplies a variable by a value and then updates the variable with the resulting product.

**Multiplication Assignment Example:**

sets the initial value of`let x = 10;`

to 10.`x`

multiplies the current value of`x *= 5;`

by 5, resulting in`x`

becoming 50.`x`

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Multiplication Assignment</h2>
<h3>The *= Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x *= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
```

## The **= Operator

The Exponentiation Assignment Operator (** **=**) in JavaScript raises a variable to the power of a specified operand and then updates the variable with the resulting value.

**Exponentiation Assignment Example:**

initializes`let x = 10;`

with the value 10.`x`

raises`x **= 5;`

to the power of 5, effectively computing 105105, which results in`x`

becoming 100000.`x`

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Exponentiation Assignment</h2>
<h3>The **= Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x **= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
```

## The /= Operator

The Division Assignment Operator (** /=**) in JavaScript divides a variable by a specified value and then updates the variable with the resulting quotient.

**Division Assignment Example:**

initializes`let x = 10;`

with the value 10.`x`

divides`x /= 5;`

by 5, resulting in`x`

becoming 2.`x`

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Division Assignment</h2>
<h3>The /= Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x /= 5;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
```

## The %= Operator

The Remainder Assignment Operator (** %=**) in JavaScript calculates the remainder of the division of a variable by a specified value and then updates the variable with that remainder.

**Remainder Assignment Example:**

initializes`let x = 10;`

with the value 10.`x`

divides`x %= 5;`

by 5 and assigns the remainder of this division to`x`

, which results in`x`

becoming 0, because 10 divided by 5 leaves no remainder.`x`

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Remainder Assignment</h2>
<h3>The %= Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x %= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
```

## The <<= Operator

The Left Shift Assignment Operator (** <<=**) in JavaScript shifts the bits of a variable to the left by a specified number of positions and assigns the result back to the variable. This bitwise operation effectively multiplies the number by 2 raised to the power of the number of shifts.

**Left Shift Assignment Example:**

initializes`let x = -100;`

with the value -100.`x`

shifts the bits of`x <<= 5;`

five positions to the left. In binary, left-shifting a number by one position is equivalent to multiplying it by 2, so shifting by 5 positions multiplies it by 2525 or 32. This operation changes the value of`x`

to -3200 (since -100 multiplied by 32 equals -3200).`x`

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Left Shift Assignment</h2>
<h3>The <<= Operator</h3>
<p id="demo"></p>
<script>
let x = -100;
x <<= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
```

The Right Shift Assignment Operator (** >>=**) in JavaScript shifts the bits of a variable to the right by a specified number of positions and assigns the result back to the variable.

**Right Shift Assignment Example:**

initializes`let x = -100;`

with the value -100.`x`

shifts the bits of`x >>= 5;`

five positions to the right. This operation divides the number by 2525 or 32, while preserving the sign of the number. Thus, -100 divided by 32 roughly equals -3 when rounded towards zero, resulting in`x`

becoming -3.`x`

## The >>>= Operator

The Unsigned Right Shift Assignment Operator (** >>>=**) in JavaScript shifts the bits of a variable to the right by a specified number of positions and assigns the result back to the variable.

**Unsigned Right Shift Assignment Example:**

initializes`let x = -100;`

with the value -100.`x`

shifts the bits of`x >>>= 5;`

five positions to the right.`x`

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Right Shift Assignment</h2>
<h3>The >>>= Operator</h3>
<p id="demo"></p>
<script>
let x = -100;
x >>>= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
```

## The &= Operator

The Bitwise AND Assignment Operator (** &=**) in JavaScript performs a bitwise AND operation between two operands and then assigns the result back to the variable. This operator compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the resulting bit is set to 1; otherwise, it is set to 0.

**Bitwise AND Assignment Example:**

initializes`let x = 10;`

with the value 10, which in binary is`x`

.`1010`

performs a bitwise AND operation with`x &= 5;`

, which in binary is`5`

.`0101`

- The operation
results in`1010 & 0101`

because none of the corresponding bits in both numbers are 1 at the same position.`0000`

- Thus, after the operation,
becomes 0.`x`

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Bitwise AND Assignment</h2>
<h3>The &= Operator</h3>
<p id="demo"></p>
<script>
let x = 100;
x &= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
```

## The |= Operator

The Bitwise OR Assignment Operator (** |=**) in JavaScript performs a bitwise OR on two operands and assigns the result to the variable.

**Example:**

`let x = 10;`

changes`x |= 5;`

to 15.`x`

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Bitwise OR Assignment</h2>
<h3>The |= Operator</h3>
<p id="demo"></p>
<script>
let x = 100;
x |= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
```

## The ^= Operator

The Bitwise XOR Assignment Operator (** ^=**) performs a bitwise XOR operation between two operands and assigns the resulting value back to the variable.

**Example:**

`let x = 10;`

changes`x ^= 5;`

to 15.`x`

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Bitwise XOR Assignment</h2>
<h3>The ^= Operator</h3>
<p id="demo"></p>
<script>
let x = 100;
x ^= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
```

## The &&= Operator

The Logical AND Assignment Operator (** &&=**) assigns the second value to the variable if the first value is truthy.

**Example:**

`let x = 10;`

results in`x &&= 5;`

being set to 5, because 10 is truthy.`x`

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Logical AND Assignment</h2>
<h3>The &&= Operator</h3>
<p>If the first value is true, the second value is assigned.</p>
<p id="demo"></p>
<script>
let x = 100;
x &&= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
```

## The ||= Operator

The Logical OR Assignment Operator (** ||=**) assigns the second value to the variable if the first value is falsy.

**Example:**

`let x = 10;`

- Since
is truthy,`x`

does not change`x ||= 5;`

, and it remains 10.`x`

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Logical OR Assignment</h2>
<h3>The ||= Operator</h3>
<p>If the first value is false, the second value is assigned:</p>
<p id="demo"></p>
<script>
let x = undefined;
x ||= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
```

## The ??= Operator

The Nullish Coalescing Assignment Operator (** ??=**) assigns the second value to the variable if the first value is either

**or**

`undefined`

**.**

`null`

**Example:**

initializes`let x;`

without a value, so it's`x`

.`undefined`

assigns`x ??= 5;`

to`5`

because`x`

was`x`

. Now,`undefined`

equals 5.`x`

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>The ??= Operator</h2>
<p>The ??= operator is used between two values. If the first value is undefined or null, the second value is assigned.</p>
<p id="demo"></p>
<script>
let x;
document.getElementById("demo").innerHTML = x ??= 5;
</script>
</body>
</html>
```