JavaScript for Beginners
About Lesson

Assignment operators are used to assign values to variables. JavaScript supports several assignment operators, including:

  • = (Assignment)
  • += (Addition assignment)
  • -= (Subtraction assignment)
  • *= (Multiplication assignment)
  • /= (Division assignment)
  • %= (Remainder assignment)

 

How to Use Assignment Operators

Here’s a brief explanation of how each operator works:

 

Assignment (=)

The = operator assigns the value on the right to the variable on the left.

let x = 5; // x is now 5

 

Addition Assignment (+=)

The += operator adds the value on the right to the variable on the left and assigns the result to the variable on the left.

let x = 5;
x += 3; // x is now 8

 

Subtraction Assignment (-=)

The -= operator subtracts the value on the right from the variable on the left and assigns the result to the variable on the left.

let x = 5;
x -= 3; // x is now 2

 

Multiplication Assignment (*=)

The *= operator multiplies the variable on the left by the value on the right and assigns the result to the variable on the left.

let x = 5;
x *= 3; // x is now 15

 

Division Assignment (/=)

The /= operator divides the variable on the left by the value on the right and assigns the result to the variable on the left.

let x = 6;
x /= 3; // x is now 2

 

Remainder Assignment (%=)

The %= operator divides the variable on the left by the value on the right and assigns the remainder to the variable on the left.

let x = 7;
x %= 3; // x is now 1

 

Understanding and using JavaScript’s assignment operators is fundamental to assigning and manipulating values in your code. They are essential tools in every developer’s toolkit.