Assignment Operators

Assignment operators assign a value to a variable.
There are six assignment operators in JavaScript that can be seen in the table below:

OperatorSymbolFunction
Assignment=Assigns the value on the right side of the operator to a variable.
Add and assign+=Add the value on the right side of the operator to the variable on the left side then assigns the new value to the variable.
Subtract and assign-=Subtract the value on the right side of the operator from the variable on the left side then assigns the new value to the variable.
Multiply and assign*=Multiplies the value on the right side of the operator by the variable on the left side then assigns the new value to the variable.
Divide and assign/=Divides the variable on the left side of the operator by the value on the left side then assigns the new value to the variable.
Modulus and assign%=Takes the integer remainder of dividing the variable on the left side by the value on the right side and assigns the new value to the variable.

The Assignment Operator (=)

The assignment operator assigns the value on the right side of the operator to the variable on the left side.


var PriceHonda = 2500;

The Add-and-Assign Operator (+=)

The Add-and-Assign Operator (+=) adds the value on the right side of the operator to the variable on the left side then assigns the new value to the variable.


var PriceHonda = 2500;
PriceHonda += 1;

The += operator in the above example adds 1, which is the value on the right side, to the variable on the left side, which in PriceHonda, and then assigns the new value (2501) to the variable PriceHonda.

Instead of adding 1 to the value of the variable PriceHonda you can add any other value.


var PriceHonda = 2500;
PriceHonda +=500;

In this situation the variable PriceHonda ends up with a value of 3000.

You can add the value of another variable instead of a number.


var PriceHonda = 2500;
var TaxValue = 400;
PriceHonda += TaxValue;

The variable PriceHonda ends up with a value of 2900 because the value of the variable TaxValue is added to the value of the variable PriceHonda then the new resulted value is assigned to the variable PriceHonda.

You can also use strings with += operator.


var MyName = "Dan ";
MyName += " Dumitrache";

The value of the variable MyName will be Dan Dumitrache.

The Subtract-and-Assign Operator (-=)

The -= operator works just like += operator but instead of adding, it subtracts.


var PriceHonda = 2500;
var DiscountValue = 400;
PriceHonda -= DiscountValue;

The value of the variable DiscountValue is subtracted from the value of the variable PriceHonda then the result is assigned to the variable PriceHonda.

The Multiply-and-Assign Operator (*=)

The Multiply-and-Assign Operator (*=) multiplies the value on the right side of the operator by the variable on the left side then assigns the new value to the variable.


var PriceHonda = 2500;
var TwoHondas = 2;
PriceHonda *= TwoHondas;

The value of the variable PriceHonda (2500) is multiplied by the value of the variable TwoHondas (2). The result (5000) is then assigned to the variable PriceHonda.

The Divide-and-Assign Operator (/=)

The Divide-and-Assign Operator (/=) divides the value on the right side of the operator by the variable on the left side then assigns the new value to the variable.


var PriceHonda = 2500;
var FiftyOff = 2;
PriceHonda /= FiftyOff;

The value of the variable PriceHonda (2500) is divided by the value of the variable FiftyOff (2). The result (1250) is then assigned to the variable PriceHonda.

The Modulus-and-Assign Operator (%=)

The Modulus-and-Assign Operator (%=) takes the integer remainder of dividing the variable on the left side by the value on the right side and assigns the new value to the variable.


var PriceHonda = 2500;
var FiftyOff = 2;
PriceHonda %= FiftyOff;

This example is the same as the previous one (The Divide-and-Assign Operator example) with a single difference; instead of /+ operator the example uses %= operator. Both examples perform a division but unlike the first example, where the result of the division is assigned to the value, in the %= example the value of the variable PriceHonda ends up to be 0. That’s because the %= operator assigns the reminder of the division as the value of the variable. Since 2500 divided by 2 is 1250 with no reminder, means that the end result of the calculation is 0.

Leave a Reply