- Lil'bots
- Learn JavaScript
- JavaScript Basics
- Assignment Operators

# Assignment Operators

Assignment operators are fundamental components in JavaScript that allow you to assign values to variables. In this blog post, we'll dive into various assignment operators that JavaScript provides, from the basic to the advanced ones. We'll use numerous code examples to make these concepts clear and easy to follow.

**Basic Assignment Operators**

**Simple Assignment (**`=`

)

The simplest and most commonly used assignment operator is the `=`

operator. It assigns the value of its right-hand operand to the left-hand operand.

**Example:**

`let a = 10; let b = 5; a = b; // a is now 5`

**Addition Assignment (**`+=`

)

Adds the value of the right operand to a variable and assigns the result to the variable.

**Example:**

`let a = 5; a += 10; // a is now 15`

For strings, the `+=`

operator concatenates the right operand to the left operand.

**Example:**

`let text = "Hello"; text += " World"; // text is now "Hello World"`

**Subtraction Assignment (**`-=`

)

Subtracts the value of the right operand from a variable and assigns the result to the variable.

**Example:**

`let a = 10; a -= 5; // a is now 5`

**Multiplication Assignment (**`*=`

)

Multiplies a variable by the value of the right operand and assigns the result to the variable.

**Example:**

`let a = 10; a *= 5; // a is now 50`

**Division Assignment (**`/=`

)

Divides a variable by the value of the right operand and assigns the result to the variable.

**Example:**

`let a = 10; a /= 2; // a is now 5`

**Remainder Assignment (**`%=`

)

Divides a variable by the value of the right operand and assigns the remainder to the variable.

**Example:**

`let a = 10; a %= 3; // a is now 1`

**Exponentiation Assignment (**`**=`

)

Raises a variable to the power of the right operand and assigns the result to the variable.

**Example:**

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

**Shift Assignment Operators**

**Left Shift Assignment (**`<<=`

)

Left shifts a variable's bits by the amount specified by the right operand.

**Example:**

`let a = 5; // Binary: 101 a <<= 1; // a is now 10 (Binary: 1010)`

**Right Shift Assignment (**`>>=`

)

Right shifts a variable's bits by the amount specified by the right operand.

**Example:**

`let a = 10; // Binary: 1010 a >>= 1; // a is now 5 (Binary: 0101)`

**Unsigned Right Shift Assignment (**`>>>=`

)

Right shifts a variable's bits by the specified amount, filling the leftmost bits with zero.

**Example:**

`let a = -10; a >>>= 1; // a is now 2147483643`

**Bitwise Assignment Operators**

**Bitwise AND Assignment (**`&=`

)

Performs a bitwise AND operation on a variable and the right operand and assigns the result to the variable.

**Example:**

`let a = 5; // Binary: 0101 a &= 3; // a is now 1 (Binary: 0001)`

**Bitwise OR Assignment (**`|=`

)

Performs a bitwise OR operation on a variable and the right operand and assigns the result to the variable.

**Example:**

`let a = 5; // Binary: 0101 a |= 3; // a is now 7 (Binary: 0111)`

**Bitwise XOR Assignment (**`^=`

)

Performs a bitwise XOR operation on a variable and the right operand and assigns the result to the variable.

**Example:**

`let a = 5; // Binary: 0101 a ^= 3; // a is now 6 (Binary: 0110)`

**Logical Assignment Operators**

Logical assignment operators are recent additions to JavaScript and were introduced in ES2020. They combine logical operations with assignments.

**Logical AND Assignment (**`&&=`

)

Assigns the right operand to the variable if the variable is truthy.

**Example:**

`let a = 10; a &&= 5; // a is now 5`

**Logical OR Assignment (**`||=`

)

Assigns the right operand to the variable if the variable is falsy.

**Example:**

`let a = 0; a ||= 5; // a is now 5`

**Nullish Coalescing Assignment (**`??=`

)

Assigns the right operand to the variable if the variable is `null`

or `undefined`

.

**Example:**

`let a = null; a ??= 5; // a is now 5`

### Start Building in JavaScript Today

Lilbots is the best platform for creating powerful JavaScript bots

- Built in access to cutting edge AI models
- Out of the box integration with hundreds of APIs
- Collaborate with coding workspaces
- Discover and fork thousands of bots built by the community