Open In App

What is the use of the += operator in JavaScript ?

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The += operator is a compound assignment operator used for addition. It adds the value of the right operand to the value of the left operand and assigns the result to the left operand.

Syntax:

a += b 

Example: Here, x += 3 is equivalent to x = x + 3. It adds 3 to the current value of x (which is 5), and assigns the result (8) back to x. Finally, console.log(x) prints the updated value of x, which is 8.

Javascript




let x = 5;
x += 3; // Equivalent to: x = x + 3;
console.log(x); // Output: 8


Output

8

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads