How Compound Assignment With Augmented Addition in JavaScript

Posted by

In JavaScript, the compound assignment operator “+=” adds the right operand to the left operand and assigns the result to the left operand. This is known as “augmented addition.”

Here’s an example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let x = 5;
x += 2;
console.log(x);  // Output: 7

[/dm_code_snippet]

In this example, the value of x is initially 5. The compound assignment operator += adds 2 to x, resulting in a new value of 7. The result of the operation is then assigned back to x, so the value of x becomes 7.

You can use the compound assignment operator += with any data type that supports addition, including numbers, strings, and arrays.

For numbers, the operator performs standard arithmetic addition. For example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let x = 5;
x += 2;
console.log(x);  // Output: 7

[/dm_code_snippet]

For strings, the operator concatenates (joins) the two strings. For example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let x = "Hello";
x += " world";
console.log(x);  // Output: "Hello world"

[/dm_code_snippet]

For arrays, the operator concatenates the two arrays. For example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let x = [1, 2, 3];
x += [4, 5, 6];
console.log(x);  // Output: [1, 2, 3, 4, 5, 6]

[/dm_code_snippet]

You can also use the compound assignment operator += with variables of different types. For example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let x = 5;
let y = "hello";
x += y;
console.log(x);  // Output: "5hello"

[/dm_code_snippet]

In this case, the number 5 is converted to a string before it is concatenated with the string “hello”.

Here are a few more examples of how the compound assignment operator += can be used in JavaScript:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

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

let y = "Hello";
y += " world";
console.log(y);  // Output: "Hello world"

let z = [1, 2, 3];
z += [4, 5, 6];
console.log(z);  // Output: [1, 2, 3, 4, 5, 6]

let a = 5;
let b = "hello";
a += b;
console.log(a);  // Output: "5hello"

[/dm_code_snippet]

As you can see, the compound assignment operator += can be used to add numbers, concatenate strings, and concatenate arrays, depending on the data types of the operands.

It’s also worth noting that the compound assignment operator += is just one of several compound assignment operators available in JavaScript. Other compound assignment operators include -=, *=, /=, and %=, which perform subtraction, multiplication, division, and modulus, respectively.

For example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let x = 10;
x -= 5;
console.log(x);  // Output: 5

let y = "Hello";
y *= 2;
console.log(y);  // Output: "HelloHello"

let z = [1, 2, 3];
z /= 2;
console.log(z);  // Output: [0.5, 1, 1.5]

let a = 8;
a %= 3;
console.log(a);  // Output: 2

[/dm_code_snippet]