Open In App

JavaScript Assignment Operators

Last Updated : 05 Jun, 2025
Suggest changes
Share
Like Article
Like
Report

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

JavaScript
// Lets take some variables x = 10 y = 20 x = y ; console.log(x); console.log(y); 

Output
20 20 

More Assignment Operators

There are so many assignment operators as shown in the table with the description.

OPERATOR NAMESHORTHAND OPERATORMEANING
Addition Assignmenta+=ba=a+b
Subtraction Assignmenta-=ba=a-b
Multiplication Assignmenta*=ba=a*b
Division Assignmenta/=ba=a/b
Remainder Assignmenta%=ba=a%b
Exponentiation Assignmenta**=ba=a**b
Left Shift Assignmenta<<=ba=a<<b
Right Shift Assignmenta>>=ba=a>>b
Bitwise AND Assignmenta&=ba=a&b
Bitwise OR Assignmenta|=ba=a | b
Bitwise XOR Assignmenta^=ba=a^b

Logical AND Assignment

a&&=b

x && (x = y)

Logical OR Assignment

||=

x || (x = y)

Nullish coalescing Assignment

??=

x ?? (x = y)

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: 4 console.log(a += b ); 

Output
2 4 

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 yoo = 4; // Expected output 3 console.log(yoo -= 1);  

Output
3 

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 yooMul = 4; // Expected output 3 console.log(yooMul *= 2);  

Output
3 

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 yooDiv = 10; const moo = 2; console.log(yooDiv /= moo);  console.log(yooDiv /= 0);  

Output
5 Infinity 

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 yooRem = 50; console.log(yooRem %= 10);  

Output
0 

Exponentiation Assignment Operator

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

Example:

JavaScript
let yooExp = 2; console.log(yooExp **= 3); 

Output
0 

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 yooShiftLeft = 5; console.log(yooShiftLeft <<= 2);  

Output
20 

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 yooShiftRight = 5; console.log(yooShiftRight >>= 2); 

Output
1 

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 yooBitwiseAnd = 5; console.log(yooBitwiseAnd &= 2); 

Output
0 

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 yooBitwiseOr = 5; console.log(yooBitwiseOr |= 2); 

Output
7 

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 yooBitwiseXor = 5; console.log(yooBitwiseXor ^= 2);  

Output
7 

Logical AND Assignment Operator(&&=)

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

Example:

JavaScript
let name = { firstName: "Ram", lastName: "" }; console.log(name.firstName);  name.firstName &&= "Shyam"; console.log(name.firstName);  console.log(name.lastName);  name.lastName &&= "Kumar"; console.log(name.lastName);  

Output
Ram Shyam

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 nameOr = { firstName: "Ram", lastName: "" }; console.log(nameOr.firstName);  nameOr.firstName ||= "Shyam"; console.log(nameOr.firstName);  console.log(nameOr.lastName);  nameOr.lastName ||= "Kumar"; console.log(nameOr.lastName);  

Output
Ram Ram Kumar 

Nullish coalescing Assignment Operator(??=)

The Nullish coalescing Assignment Operator assigns the value of y to x if the value of x is null.

Example:

JavaScript
let xNull = 12; let yNull = null; let zNull = 13; xNull ??= zNull; yNull ??= zNull; console.log(xNull);  console.log(yNull);  

Output
12 13 


Supported Browsers: The browsers supported by all JavaScript Assignment operators are listed below:

  • Google Chrome
  • Firefox
  • Opera
  • Safari
  • Microsoft Edge

Next Article

Similar Reads