Compound Assignment with Arithmetic Operation

Compound Assignment with Arithmetic Operation

·

2 min read

Hello everyone hope everyone is doing well. My name is Surya L, the purpose of this blog is to teach all about Compound Assignment with Arithmetic Operations.

Content:

  • 1.Compound Assignment With Augmented Addition

  • 2.Compound Assignment With Augmented Subtraction

  • 3.Compound Assignment With Augmented Multiplication

  • 4.Compound Assignment With Augmented Division

1.Compound Assignment With Augmented Addition:

Assignments are commonly used to modify the contents of a variable in programming. All things to the right of the equals sign are evaluated first, so we can say

myVar = myVar + 5;
//to add 5 to myVar. Since this is such a common pattern, there are operators which do both a mathematical operation and assignment in one step.

//One such operator is the **+= **operator.

let myVar = 1;
myVar += 5;
console.log(myVar);

2.Compound Assignment With Augmented Subtraction

Like the += operator above, we can use -= subtracts a number from a variable.

myVar = myVar - 5;
//will subtract 5 from myVar. This can be rewritten as:

myVar -= 5;

3.Compound Assignment With Augmented Multiplication

The *= operator multiplies a variable by a number.

myVar = myVar * 5;
//will multiply myVar by 5. This can be rewritten as:

myVar *= 5;

4.Compound Assignment With Augmented Division

The /= operator divides a variable by another number.

myVar = myVar / 5;
Will divide myVar by 5. This can be rewritten as:

myVar /= 5;

To Summarize:

  • Compound Assignment With Augmented Addition(+=):Adds the value of variable by value given(eg myVar+=5)

  • Compound Assignment With Augmented Subtraction(-=):Subtract the value of variable by value given(eg myVar-=5)

  • Compound Assignment With Augmented Multiplication(=)Multiply the value of variable by value given(eg myVar=5)

  • Compound Assignment With Augmented Division(/=):Divide the value of variable by value given(eg myVar/=5)

Credits: I learned this topics in FreeCodeCamp which I explained in minified version

Thanks for reading the blog. Do let me know what you think about it.

Did you find this article valuable?

Support Surya's Web Blogs by becoming a sponsor. Any amount is appreciated!