JavaScript Assignment Operators
Last Updated :
21 Nov, 2024
Assignment operators are used to assign values to variables in JavaScript.
JavaScript
// Lets take some variables
x = 10
y = 20
x = y // Here, x is equal to 20
console.log(x);
console.log(y);
More Assignment Operators
There are so many assignment operators as shown in the table with the description.
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 + 1);
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(foo = yoo - 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 yoo = 4;
// Expected output 3
console.log(foo = yoo - 1);
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 yoo = 10;
const moo = 2;
// Expected output 5
console.log(yoo = yoo / moo);
// Expected output Infinity
console.log(yoo /= 0);
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 yoo = 50;
// Expected output 0
console.log(yoo %= 10);
Exponentiation Assignment Operator
The Exponentiation Assignment Operator raises the value of a variable to the power of the right operand.
Example:
JavaScript
let yoo = 50;
// Expected output 0
console.log(yoo %= 10);
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 yoo = 5;
// Expected output 20(In Binary 10100)
console.log(yoo <<= 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 yoo = 5;
// Expected Output 1(In binary 001)
console.log(yoo >>= 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 yoo = 5;
// Expected output 0(In binary 000)
console.log(yoo &= 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 yoo=5;
// Expected output 7(In binary 111)
console.log(yoo|=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 yoo = 5;
// Expected output 7(In binary 111)
console.log(yoo ^= 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 name = {
firstName: "Ram",
lastName: "",
};
console.log(name.firstName);
// Changing the value using logical
// AND assignment operator
name.firstName &&= "Shyam";
// Here the value changed because
// name.firstName is truthy
console.log(name.firstName);
console.log(name.lastName);
// Changing the value using logical
// AND assignment operator
name.lastName &&= "Kumar";
// Here the value remains unchanged
// because name.lastName is falsy
console.log(name.lastName);
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 name = {
firstName: "Ram",
lastName: "",
};
console.log(name.firstName);
// Changing the value using logical
// OR assignment operator
name.firstName ||= "Shyam";
// But value does not change because
// name.firstName is truthy
console.log(name.firstName);
console.log(name.lastName);
// Changing the value using logical
// OR assignment operator
name.lastName ||= "Kumar";
// The value changes because name.lastName is falsy
console.log(name.lastName);
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 x = 12;
let y = null;
let z = 13;
// The value of x will become
// unchanged because x is not nullish.
x ??= z;
// The value of y will be
// changed because y is nullish.
y ??= z;
console.log(x) // 12
console.log(y) // 13
Supported Browsers: The browsers supported by all JavaScript Assignment operators are listed below:
JavaScript Assignment Operators – FAQs
What are assignment operators in JavaScript?
Assignment operators in JavaScript are used to assign values to variables. The most common assignment operator is the equals sign (=), but there are several other assignment operators that perform an operation and assign the result to a variable in a single step.
What is the basic assignment operator?
The basic assignment operator is =. It assigns the value on the right to the variable on the left.
What are compound assignment operators?
Compound assignment operators combine a basic arithmetic or bitwise operation with assignment. For example, += combines addition and assignment.
What does the += operator do?
The += operator adds the value on the right to the variable on the left and then assigns the result to the variable.
How does the *= operator work?
The *= operator multiplies the variable by the value on the right and assigns the result to the variable.
Can you use assignment operators with strings?
Yes, you can use the += operator to concatenate strings.
What is the difference between = and ==?
The = operator is the assignment operator, used to assign a value to a variable. The == operator is the equality operator, used to compare two values for equality, performing type conversion if necessary.
What does the **= operator do?
The **= operator performs exponentiation (raising to a power) and assigns the result to the variable.