- Syntax, Code & Explanation
- Other Related Concepts
This operator is not only used to add numerical values but can also be used to concatenate strings together. Essentially it replaces the variable = a + b syntax with a += b.
Syntax, Code & Explanation:The syntax of the assignment operator is quite straightforward and does not require any prerequisites.
In case you are still confused, I shall break it down even further. This method is just a shorter way of writing the following code.
x += y
Here we are again taking the value on the right (y) and adding & assigning it to the value on the left (x). The assignment operator does the same in a more concise method.
x = x + y
The Output of the above code snippet is as follows.
As you can see, the values of ‘y’ & ‘b’ have been added to the ‘x’ & ‘a’ respectively.
> 7 > "Hello World"
Once you have familiarized yourself with the addition assignment operator (+=) I would recommend practicing the other assignment operators. There are seven in total ( =, +=, -=, *=, /=, %=, **= ), and mastering them all would help you go a long way.