JavaScript Assignment Operators (original) (raw)

Last Updated : 11 Oct, 2025

Assignment operators are used to assign values to variables in JavaScript.

**Example:

JavaScript `

// Lets take some variables x = 10 y = 20

x = y ; console.log(x); console.log(y);

`

Now, we’ll explore the assignment operators one by one to understand how each of them works.

**Addition Assignment Operator(+=)

The Addition assignment operator adds the value to the right operand to a variable and assigns the result to the variable. Addition or concatenation is possible. In case of concatenation then we use the string as an operand.

**Example:

JavaScript `

let a = 2; const b = 3;

// Expected output: 2 console.log(a);

// Expected output: 5 console.log(a += b );

`

**Subtraction Assignment Operator(-=)

The Substraction Assignment Operator subtracts the value of the right operand from a variable and assigns the result to the variable.

**Example:

JavaScript `

let a = 4;

// Expected output 3 console.log(a -= 1);

`

**Multiplication Assignment Operator(*=)

The Multiplication Assignment operator multiplies a variable by the value of the right operand and assigns the result to the variable.

**Example:

JavaScript `

let a = 4;

// Expected output 8 console.log(a *= 2);

`

**Division Assignment Operator(/=)

The Division Assignment operator divides a variable by the value of the right operand and assigns the result to the variable.

**Example:

JavaScript `

let a = 10; const b = 2; console.log(a /= b);

`

**Remainder Assignment Operator(%=)

The Remainder Assignment Operator divides a variable by the value of the right operand and assigns the remainder to the variable.

**Example:

JavaScript `

let a = 50; console.log( a %= 10);

`

**Exponentiation Assignment Operator

The Exponentiation Assignment Operator raises the value of a variable to the power of the right operand.

**Example:

JavaScript `

let a = 2; console.log(a **= 3);

`

**Left Shift Assignment Operator(<<=)

This **Left Shift Assignment Operator moves the specified amount of bits to the left and assigns the result to the variable.

**Example:

JavaScript `

let a = 5; console.log(a <<= 2);

`

**Right Shift Assignment Operator(>>=)

The **Right Shift Assignment Operator moves the specified amount of bits to the right and assigns the result to the variable.

**Example:

JavaScript `

let a = 5; console.log(a >>= 2);

`

**Bitwise AND Assignment Operator(&=)

The **Bitwise AND Assignment Operator uses the binary representation of both operands, does a bitwise AND operation on them, and assigns the result to the variable.

**Example:

JavaScript `

let a = 5; console.log(a &= 2);

`

**Btwise OR Assignment Operator(|=)

The **Btwise OR Assignment Operator uses the binary representation of both operands, does a bitwise OR operation on them, and assigns the result to the variable.

**Example:

JavaScript `

let a = 5; console.log(a |= 2);

`

**Bitwise XOR Assignment Operator(^=)

The **Bitwise XOR Assignment Operator uses the binary representation of both operands, does a bitwise XOR operation on them, and assigns the result to the variable.

**Example:

JavaScript `

let a = 5; console.log(a ^= 2);

`

Logical AND Assignment Operator(&&=)

The Logical AND Assignment assigns the value of **y into **x only if **x is a **truthy value.

**Example:

JavaScript `

let x = 10; let y = 5;

x &&= y;
console.log(x);

`

Logical OR Assignment Operator(||=**)**

The **Logical OR Assignment Operator is used to assign the value of y to x if the value of x is falsy.

**Example:

JavaScript `

let x = 0; let y = 10;

x ||= y; console.log(x);

`

**Nullish coalescing Assignment Operator(??=)

The **Nullish coalescing AssignmentOperator assigns the value of y to x if the value of x is null.

**Example:

JavaScript `

let x = 12; let y = null; let z = 13;

x ??= z; y ??= z;

console.log(x);
console.log(y);

`