w3resource

JavaScript: Assignment Operators

Assignment Operators

An assignment operator assigns a value to its left operand based on the value of its right operand. The first operand must be a variable and basic assignment operator is equal (=), which assigns the value of its right operand to its left operand. That is, a = b assigns the value of b to a.

 In addition to the regular assignment operator "=" the other assignment operators are shorthand for standard operations, as shown in the following table.

Shorthand Expression Description
a +=b a = a + b Adds 2 numbers and assigns the result to the first.
a -= b a = a - b Subtracts 2 numbers and assigns the result to the first.
a *= b a = a*b Multiplies 2 numbers and assigns the result to the first.
a /=b a = a/b Divides 2 numbers and assigns the result to the first.
a %= b a = a%b Computes the modulus of 2 numbers and assigns the result to the first.
a<<=b a = a<<b Performs a left shift and assigns the result to the first operand.
a>>=b a = a>>b Performs a sign-propagating right shift and assigns the result to the first operand.
a>>>=b a = a>>>b Performs a zero-fill right shift and assigns the result to the first operand.
a&= b a = a&b Performs a bitwise AND and assigns the result to the first operand.
a^= b a = a^b Performs a bitwise XOR and assigns the result to the first operand.
a |=b a = a|b Performs a bitwise OR and assigns the result to the first operand.

Previous: JavaScript: Arithmetic Special Operators (%, ++, --, - )
Next: JavaScript: Bitwise Operators

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/javascript/operators/assignment-operator.php