Assignment Operators in JavaScript

An assignment operator requires two operands. The value of the right operand is assigned to the left operand. The sign = denotes the simple assignment operator. JavaScript also has several compound assignment operators, which is actually shorthand for other operators. A list of all such operators are listed below.

List of Assignment operators

NameSyntaxMeaning
Assignmentx = yx = y
Addition assignmentx += yx =x+ y
Subtraction assignmentx -= yx = x - y
Multiplication assignmentx *= yx = x * y
Division assignmentx /= yx = x / y
Remainder assignmentx %= yx = x % y
Exponentiation assignmentx **= yx = x ** y
Left shift assignmentx <<= yx = x << y
Right shift assignmentx >>= yx = x >> y
Unsigned right shift assignmentx >>>= yx = x >>> y
Bitwise AND assignmentx &= yx = x & y
Bitwise XOR assignmentx ^= yx = x ^ y
Bitwise OR assignmentx |= yx = x | y
Logical AND assignmentx &&= yx && (x = y)
Logical OR assignmentx ||= yx || (x = y)
Logical nullish assignmentx ??= yx ?? (x = y)

Simple Assignement Operator

An = assignment operator assigns the value of the right-hand operand to the left-hand variable.

Compound Assignment Operators

All operators except = listed in the table above are compound assignment operators. They are actually shorthand notations

For Examples

The Addition assignment += operator adds a value to a variable.

is actually shorthand for

Reference

  1. Expressions & Operators
  2. Precedency & Associativity

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top