What is the use of the += operator in JavaScript ?
Last Updated :
12 Feb, 2024
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;
console.log(x);
|
Share your thoughts in the comments
Please Login to comment...