The += operator is a shorthand for the addition assignment operator. It is used to add a value to a variable. The value can be an integer, floating point number, string, or object.
The addition assignment or the += operator does the addition and assignment in a single operation.
To learn how it works, let’s first try to separate the assignment and the increment.
let a = 1;
a = a + 2;
console.log(a);
Output:
3
The example above creates the variable a equal to 1 and then increments the value of it by 2.
This example can be rewritten using the += operator.
let a = 1;
a += 2;
console.log(a);
The output remains the same:
3