For most programming languages such as Java, C, C++, C#, PHP and including JavaScript you might come across += operator inside a program.

How += Operator Works?

If you are a beginner in programming then += might be a little confusing to you. But you don’t need to worry, it is simple and easy to understand.

The operator is simply a short notation for adding a variable with another variable/value and then storing it in the same variable.
Syntax:

x+=y   //x=x+y

JavaScript Coding Example for += Operator

Example 1:
If you want a number to be added to another value/itself and stored in the same variable.

This is a general way of doing the addition.

let num=1;
num=num+num;

// print 2 as output on console
console.log(num);

In the above code at line number 2, the same line can be written in a more shorthand version using += addition operator.


num+=num;  // shorhand for num=num+num;

Example 2:


let val=5;
val=val+50;

// print 55 as output on console
console.log(val);

Likewise, you can write the shorthand version of the above code using += as below-


val+=50; // shorhand for val=val+50;

Thus, the += operator is an additional assignment operator.

✌️ Like this article? Follow us on Facebook and LinkedIn. You can also subscribe to our weekly Feed