How to assign a incremental value to a variable in javascript?
Different assignment operators and their purpose?
Operator Syntax | Description | Example |
= | used to assign a value on the right side to the variable of the left side of the operator. | b = 3; |
+= | it adds the value on the right to the previous value of the variable on the left and assign the new value to the variable. | b = 3; b += 4; // Now b will become 7 |
-= | it subtracts the value on the right to the previous value of the variable on the left and assign the new value to the variable. | b = 4; b -= 2; // Now b will become 2 |
*= | it multiplies the operand on the right to the previous value of the variable on the left and assaigns the new value to the variable. | b = 4; b *= 2; // Now b will become 8 |
/= | it divides the operand on the right to the previous value of the variable on the left and assaigns the new value to the variable. | b = 6; b /= 2; // Now b will become 3 |
<<= | The operand on the left is shifted left by the value on the right. | - |
<<= | A signed right shifted is performed on the left operand by the value on the right. | - |