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.



Follow us on Facebook and Twitter for latest update.

JavaScript: Tips of the Day

Returns the sum of an array, after mapping each element to a value using the provided function

Example:

const tips_sumBy = (arr, fn) =>
  arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => acc + val, 0);

console.log(tips_sumBy([{ n: 2 }, { n: 4 }, { n: 6 }, { n: 8 }], o => o.n));
console.log(tips_sumBy([{ n: 1 }, { n: 3 }, { n: 5 }, { n: 7 }], 'n'));

Output:

20
16