# JavaScript Assignment Operators

An assignment operator assigns a value to the variable on the left side, depending on the value on the right. The simple assignment operator uses equal (=) to assign the value on the right side to the variable on the left side.

For example, an assignment in x = y is used to assign the value in variable y to variable x.

The following table shows the compound and abbreviated uses of assignment operators.

 Assignment `x = y` `x = y` Addition assignment `x += y` `x = x + y` Subtraction assignment `x -= y` `x = x - y` Multiplication assignment `x *= y` `x = x * y` Division assignment `x /= y` `x = x / y` Remainder assignment `x %= y` `x = x % y` Exponentiation assignment `x **= y` `x = x ** y` Left shift assignment `x <<= y` `x = x << y` Right shift assignment `x >>= y` `x = x >> y` Unsigned right shift assignment `x >>>= y` `x = x >>> y` Bitwise AND assignment `x &= y` `x = x & y` Bitwise XOR assignment `x ^= y` `x = x ^ y` Bitwise OR assignment `x |= y` `x = x | y`

### Example Usage

Assignment Process

``````// Assuming the value of the variables is as follows
// x = 5
// y = 10
// z = 25

x = y // x be 10
x = y = z // x, y and z are now 25``````

Removing Process

``````// Assuming the variable as follows
// bar = 5

bar * = 2 // 10
bar * = "foo" // NaN``````

Division

``````// Assuming the variable as follows
// bar = 5

bar / = 2 // 2.5
bar / = "foo" // NaN
bar / = 0 // Infinity``````

Remaining operation

``````// Assuming the variable as follows
// bar = 5

bar% = 2 // 1
bar% = "foo" // NaN
bar% = 0 // NaN``````

exponent

``````// Assuming the variable as follows
// bar = 5

bar ** = 2 // 25
bar ** = "foo" // NaN``````

Left Scrolling

``````var bar = 5; // (00000000000000000000000000000101)
bar <<= 2; // 20 (00000000000000000000000000010100)``````

Right Scrolling

``````var bar = 5; // (00000000000000000000000000000101)
bar >>= 2; // 1 (00000000000000000000000000000001)

var bar -5; // (-00000000000000000000000000000101)
bar >>= 2; // -2 (-00000000000000000000000000000010)``````

Unmarked Right Scroll

``````var bar = 5; // (00000000000000000000000000000101)
bar >>>= 2; // 1 (00000000000000000000000000000001)

var bar = -5; // (-00000000000000000000000000000101)
bar >>>= 2; // 1073741822 (00111111111111111111111111111110)``````

Bit- AND

``````var bar = 5;
// 5: 00000000000000000000000000000101
// 2: 00000000000000000000000000000010
bar &= 2; // 0``````

Bit XOR

``````var bar = 5;
bar ^= 2; // 7
// 5: 00000000000000000000000000000101
// 2: 00000000000000000000000000000010
// -----------------------------------
// 7: 00000000000000000000000000000111``````

Bit OR

``````var bar = 5;
bar |= 2; // 7
// 5: 00000000000000000000000000000101
// 2: 00000000000000000000000000000010
// -----------------------------------
// 7: 00000000000000000000000000000111``````

EXAMPLE

This example exercises the = and += operators. Each loop is functionally the same.

``````function p(stuff) {
print("<<<" + stuff + ">>>");
}

p("start =");
var x = 0.1;
do {
p("x = " + x);
x = x + 0.1;
} while(x < 1.0)

p("start +=");
var x = 0.1;
do {
p("x = " + x);
x += 0.1;
} while(x < 1.0)``````

This example exercises the assignment operators that also perform arithmetic operations.

``````function p(stuff) {
print("<<<" + stuff + ">>>");
}

var x1 = 1.0, x2 = 1.0, x3 = 1.0, x4 = 1.0;
var y = 0.25;
for(var i = 0; i < 5; i++) {
x1 += y;
x2 -= y;
x3 *= y;
x4 /= y;
}
p("x1 += y: " + x1);
p("x2 -= y: " + x2);
p("x3 *= y: " + x3);
p("x4 /= y: " + x4);``````

This site uses Akismet to reduce spam. Learn how your comment data is processed.