JavaScript Assignment Operators

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

JavaScript provides a range of assignment operators to manipulate variables and values. Assignment operator in js is an element that helps in the process of assigning values to variables. It is a symbol or a combination of symbols used to assign a value on the right side to a variable on the left side. The equal sign(=) is the mostly used and common assignment operator. Let us explore the syntax, parameters, and examples of different assignment operators in JavaScript.

Syntax for Assignment Operator in JS

The assignment operator in JavaScript is represented by the equal sign (=). The syntax follows a pattern where a variable on the left is assigned a value on the right. The syntax for assignment operator in js is:

Parameters for Assignment Operator in JS

Assignment operators in JavaScript take two operands:

  • variable:
    This is the identifier representing the storage location (memory) where the value will be stored.
  • value:
    This is the expression or literal value that is assigned to the variable.

The =(equal sign) is the assignment operator and signifies that the value on the right side will be assigned to the variable on the left side.

Return Value for Assignment Operator in JS

The assignment operation in js has no specific return value and is used for the purpose of assigning a value to a variable.

Example for Assignment Operator in JS

Let us consider the following example to illustrate the working of the assignment operator in javascript.

Explanation

In this line, a variable is the decalared to holds the value Learning. It serves as a reference to a location in memory where data is stored. We can use the let, const, or var keyword, followed by a variable name for declaring a variable. Explore the difference between the three different keywords.

The equal sign is the assignment operator. It signifies that the value on the right side of the expression will be assigned to the variable on the left. Now, see the following line,

The assignment operator in js is used again to update the value of the scaler variable.

Output

Initially, the scaler variable is assigned the value "Learning," which is printed first and then it is updated to "Courses for learning" using the assignment operator.

JavaScript Assignment Operators

JS Assignment Operators

Operator SymbolNameExampleExpanded Form
=Assignmenta = 1;a = 1;
+=Addition assignmentx += 3;x = x + 3;
-=Subtraction assignmentx -= 3;x = x - 3;
*=Multiplication assignmentx *= 3;x = x * 3;
**=Exponentiation assignmentx **= 3;x = x ** 3;

Shift Assignment Operators

Operator SymbolNameExampleExpanded Form
<<=Left shift assignmentx <<= 3;x = x << 3;
>>=Right shift assignmentx >>= 3;x = x >> 3;
>>>=Unsigned right shift assignmentx >>>= 3;x = x >>> 3;

Bitwise Assignment Operators

Operator SymbolNameExampleExpanded Form
&=Bitwise AND assignmentx &= 3;x = x & 3;
|=Bitwise OR assignmentx |= 3;x = x | 3;
^=Bitwise XOR assignmentx ^= 3;x = x ^ 3;

Logical Assignment Operators

Operator SymbolNameExampleExpanded Form
&&=Logical AND assignmentx &&= true;x = x && true;
||=Logical OR assignmentx ||= true;x = x || true;
??=Nullish coalescing assignmentx ??= 3;x = x !== null && x !== undefined ? x : 3;

Examples

The = Operator

The assignment operator in js is used to assign a value to a variable.

The += Operator

The addition assignment operator adds the value on the right side to the existing value of the variable on the left side.

The -= Operator

The subtraction assignment operator subtracts the value on the right side from the existing value of the variable on the left side.

The *= Operator

The multiplication assignment operator multiplies the existing value of the variable on the left side by the value on the right side.

The **= Operator

The exponentiation assignment operator raises the existing value of the variable on the left side to the power of the value on the right side.

Example:

Explanation:

In this example, b initially has the value 2. The **= operator raises b to the power of 3. After the operation, b becomes 8.

The /= Operator

The division assignment operator ivides the existing value of the variable on the left side by the value on the right side.

The %= Operator

The remainder assignment operator calculates the remainder when dividing the existing value of the variable on the left side by the value on the right side.

The <<= Operator

The left shift assignment operator shifts the bits of the existing value of the variable on the left side to the left by the number of positions specified on the right side.

Example:

Explanation:

Here, a initially has the binary value 1000 (8 in decimal). The <<= operator shifts the bits to the left by 2 positions, resulting in 001000 in binary, which is 32 in decimal.

The >>= Operator

The right shift assignment operator shifts the bits of the existing value of the variable on the left side to the right by the number of positions specified on the right side.

Example:

Explanation:

In this example, b initially has the binary value 10000 (16 in decimal). The >>= operator shifts the bits to the right by 2 positions, resulting in 00100 in binary, which is 4 in decimal.

The >>>= Operator

The unsigned right shift assignment operator shifts the bits of the existing value of the variable on the left side to the right by the number of positions specified on the right side, filling the leftmost positions with zeros.

Example:

Explanation of Example:

Here, c initially has the binary representation with sign bits 0100 (4 in decimal). The >>>= operator shifts the bits to the right by 2 positions, filling the leftmost positions with zeros. After the operation, c becomes 1 in decimal.

The &= Operator

The bitwise AND assignment operator performs a bitwise AND operation between the existing value of the variable on the left side and the value on the right side, updating the variable with the result.

Example:

Explanation:

In this example, x initially has the binary value 101 (5 in decimal). The &= operator performs a bitwise AND operation with 3 (binary 011). The result of the operation is 1 in decimal or 001 in binary. After the operation, x becomes 1.

The |= Operator

The bitwise OR assignment operator in javascript performs a bitwise OR operation between the existing value of the variable on the left side and the value on the right side, updating the variable with the result.

Example:

Explanation:

Here, x initially has the binary value 1000 (8 in decimal). The |= operator performs a bitwise OR operation with 3 (binary 100). The result is 1100 in binary, which is 12 in decimal. After the operation, y becomes 12.

The ^= Operator

The bitwise XOR assignment operator in js performs a bitwise XOR operation between the existing value of the variable on the left side and the value on the right side, updating the variable with the result.

Example:

Explanation: In this example, z initially has the binary value 101 (5 in decimal). The ^= operator performs a bitwise XOR operation with 3 (binary 011). The result is 110 in binary, which is 6 in decimal. After the operation, z becomes 6.

The &&= Operator

The logical AND assignment operator checks if the current value of the variable on the left side is truthy. If it is, it updates the variable with the right operand. Explore about truthy and fasly in Javascript.

The ||= Operator

The logical OR assignment operator checks if the current value of the variable on the left side is false. If it is, it updates the variable with the right operand.

The ??= Operator

The ??= operator is the nullish coalescing assignment operator. It checks if the current value of the variable on the left side is null or undefined. If it is, it updates the variable with the right operand.

Example:

Explanation:

Here, c initially has the value null. The ??= operator checks if c is null or undefined. Since it is, c is updated with the right operand (2). After the operation, c becomes 2.

Conclusion

  • Assignment operators in js are essential for assigning values to variables. Assigning a value to a variable can be done using the assignment operator (=), while others like +=, -= offer shorthand ways of performing addition or subtraction with assignment.
  • Shift assignment operators (<<=, >>=, >>>=) are useful for bitwise operations, manipulating binary representations of integers.
  • Bitwise assignment operators (&=, |=, ^=) perform bitwise AND, OR, XOR operations with assignment, useful for bit manipulation.
  • Logical assignment operators (&&=, ||=, ??=) combine logical operations with assignment, offering a easier ways to update variable values based on conditions.