JavaScript: Assignment Operators

Profile picture for user arilio666

If you want to assign a value to a variable, then the assignment operator is used. It is used to assign values or do some operations to the values assigned to a variable.

1) = Operator

The = Operator assigns the values to the variables when declaring.

x = 99;

2) += Operator 

The += Operator adds the value inside the variable, or we can say which is assigned.

let x = 100;
x += 100;

console.log(x)

Output:

200

3) -= Operator

The -= Operator subtracts the values inside a variable and is relatively identical and opposite of what the += does.

let x = 100;
x -= 100;

console.log(x)

Output:

0

4) *= Operator

The *= Operator multiplies the values inside the variable.

let x = 100;
x *= 100;

console.log(x)

Output:

10000

5) /= Operator

The /= Divides the values inside the variable.

let x = 100;
x /= 100;

console.log(x)

Output:

1

6) %= Operator

The %= operator gives out the reminder of the values inside the variable, or we can say it gives out the leftover of the two values after division.

let x = 100;
x %= 100;

console.log(x)

Output:

0