JavaScript Assignment

JavaScript Assignment Operators

Assignment operators assign values to JavaScript variables.

Operator Example Same As
= x = y x = y
+= x += y x = x + y
-= x -= y x = x - y
*= x *= y x = x * y
/= x /= y x = x / y
%= x %= y x = x % y
<<= x <<= y x = x << y
>>= x >>= y x = x >> y
>>>= x >>>= y x = x >>> y
&= x &= y x = x & y
^= x ^= y x = x ^ y
|= x |= y x = x | y
**= x **= y x = x ** y

The **= operator is a part of ECMAScript 2016.

Assignment Examples

The = assignment operator assigns a value to a variable.

Assignment

let x = 10;
Try it Yourself »

The += assignment operator adds a value to a variable.

Assignment

let x = 10;
x += 5;
Try it Yourself »

The -= assignment operator subtracts a value from a variable.

Assignment

let x = 10;
x -= 5;
Try it Yourself »

The *= assignment operator multiplies a variable.

Assignment

let x = 10;
x *= 5;
Try it Yourself »

The /= assignment divides a variable.

Assignment

let x = 10;
x /= 5;
Try it Yourself »

The %= assignment operator assigns a remainder to a variable.

Assignment

let x = 10;
x %= 5;
Try it Yourself »

Exercise:

Use the correct assignment operator that will result in x being 15 (same as x = x + y).

x = 10;
y = 5;
x  y;

Start the Exercise