JavaScript Assignment

Assignment operators assign values to JavaScript variables

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;

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

Assignment

let x = 10;
x += 5;

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

Assignment

let x = 10;
x -= 5;

The *= assignment operator multiplies a variable.

Assignment

let x = 10;
x *= 5;

The /= assignment divides a variable.

Assignment

let x = 10;
x /= 5;

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

Assignment

let x = 10;
x %= 5;

Test Yourself With Exercises

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