Assignment Operators

Assignment operators are used to assign values to variables.

OperatorMeaningExampleSame As
=Assignx = 5
+=Add and assignx += 3x = x + 3
-=Subtract and assignx -= 2x = x – 2
*=Multiply and assignx *= 2x = x * 2
/=Divide and assignx /= 2x = x / 2
%=Modulus and assignx %= 2x = x % 2
Assignment Operators

= (Assignment)

This operator assigns a value to a variable.

let x = 5;

The value on the right side is stored in the variable on the left side.

You can also assign results of expressions:

let x = 10 + 5; // 15

Assignment always works right → left.


+= (Add and Assign)

This adds a value to the variable and updates it.

let x = 10;
x += 5; // x = x + 5
console.log(x); // 15

It works exactly like writing:

x = x + value;

Also follows + behavior (can concatenate strings):

let x = "Hello ";
x += "World";
console.log(x); // "Hello World"

-= (Subtract and Assign)

This subtracts a value from the variable and updates it.

let x = 10;
x -= 3; // x = x - 3
console.log(x); // 7

It always converts values to numbers.

let x = "10";
x -= 2;
console.log(x); // 8

*= (Multiply and Assign)

This multiplies the variable by a value and stores the result.

let x = 10;
x *= 2; // x = x * 2
console.log(x); // 20

Works with type conversion:

let x = "5";
x *= 2;
console.log(x); // 10

/= (Divide and Assign)

This divides the variable by a value and updates it.

let x = 10;
x /= 2; // x = x / 2
console.log(x); // 5

Special cases:

let x = 10;
x /= 0;
console.log(x); // Infinity

%= (Modulus and Assign)

This stores the remainder after division.

let x = 10;
x %= 3; // x = x % 3
console.log(x); // 1

Useful in cycles or patterns:

let x = 5;
x %= 2;
console.log(x); // 1

📣 Follow us for more updates:

Follow on LinkedIn Join WhatsApp Channel
Scroll to Top