Skip to content

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.

Assignmentx = yx = y
Addition assignmentx += yx = x + y
Subtraction assignmentx -= yx = x - y
Multiplication assignmentx *= yx = x * y
Division assignmentx /= yx = x / y
Remainder assignmentx %= yx = x % y
Exponentiation assignmentx **= yx = x ** y
Left shift assignmentx <<= yx = x << y
Right shift assignmentx >>= yx = x >> y
Unsigned right shift assignmentx >>>= yx = x >>> y
Bitwise AND assignmentx &= yx = x & y
Bitwise XOR assignmentx ^= yx = x ^ y
Bitwise OR assignmentx |= yx = 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);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: