Looking for Javascript += operator?
For most programming languages such as Java, C, C++, C#, PHP and including JavaScript you might come across += operator inside a program. += is an assignment that is used to add the value of the right side operand to a variable and assign the variable to the result.
How does JavaScript += 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 javascript += as below-
val+=50; // shorhand for val=val+50;
Thus, the javascript += operator is an additional assignment operator.
Read more JavaScript tutorials-
- JavaScript to Lowercase
- Node JS Post Request
- JavaScript Try Catch
- JavaScript For Loop
- Add days to a date in JavaScript
Like this article? Follow us on Facebook and LinkedIn. You can also subscribe to our weekly Feed