Assignment Operators

Learn how to use assignment operators in JavaScript to store and update values in variables with simple real-life examples.

What is an Assignment Operator?Link to this section

An assignment operator is a special symbol used to assign a value to a variable. The most basic assignment operator is the equals sign, which takes the value on its right and stores it into the variable on its left.

Stores the number 10 inside the variable named score.

  • `let score` is the variable declaration
  • `=` is the **assignment operator**
  • `10` is the value being assigned
  • `console.log` prints the stored value

Assignment operators are used in almost every JavaScript program to update the state of variables as data changes.

Why Do We Need Assignment Operators?Link to this section

We need assignment operators because programs constantly need to save new data, update scores, and track changes over time.

  • Track changing user scores in a game
  • Update the total price of items in a shopping cart
  • Accumulate total marks scored across multiple tests
  • Count the number of times a user clicks a button
  • Adjust a bank account balance after a deposit or withdrawal
  • Keep track of remaining inventory in a shop

Adds 20 to the existing bill and saves the new total.

This example shows how we take the current value of a variable, modify it, and save it back using basic math and assignment.

About Assignment OperatorsLink to this section

Assignment operators work by evaluating the expression on the right-hand side first, and then putting that result into the variable on the left-hand side. JavaScript provides shorthand operators that combine arithmetic operations with assignment to make code shorter and easier to read. These operators follow specific rules of precedence, meaning standard assignment happens from right to left.

OperatorNameExampleSame As
=Assignmentx = yx = y
+=Addition assignmentx += yx = x + y
-=Subtraction assignmentx -= yx = x - y

Using += is a shorter way to write points = points + 10.

Types of Assignment OperatorsLink to this section

1. Basic Assignment Operator (`=`)Link to this section

The basic assignment operator places the value of its right operand into its left operand. It is the foundation of all variable updates.

note

The variable must be declared using let, var, or const before you can assign a value to it.

2. Addition Assignment Operator (`+=`)Link to this section

The addition assignment operator adds the value on the right to the variable on the left and assigns the result to the variable.

tip

Use `+=` when building up strings or accumulating totals in a loop to keep your code clean.

3. Subtraction Assignment Operator (`-=`)Link to this section

The subtraction assignment operator subtracts the value on the right from the variable on the left and assigns the result to the variable.

warning

Make sure the variable is declared with `let`, not `const`, because its value will change.

4. Multiplication Assignment Operator (`*=`)Link to this section

The multiplication assignment operator multiplies the variable by the value on the right and assigns the result to the variable.

note

This operator is useful for scaling values up, such as applying bulk multipliers or scaling game scores.

5. Division Assignment Operator (`/=`)Link to this section

The division assignment operator divides the variable by the value on the right and assigns the result to the variable.

warning

Dividing a number by zero using `/=` will result in `Infinity` rather than crashing your program.

6. Remainder Assignment Operator (`%=`)Link to this section

The remainder assignment operator divides the variable by the value on the right and assigns the remainder to the variable.

note

Great for cycling through fixed ranges or checking even and odd numbers.

7. Exponentiation Assignment Operator (`**=`)Link to this section

The exponentiation assignment operator raises the variable to the power of the value on the right.

tip

Equivalent to multiplying a number by itself multiple times, useful in mathematical calculations.

Chaining AssignmentsLink to this section

You can chain multiple assignment operators together in a single statement. JavaScript evaluates the assignment from right to left, assigning the final value to all variables in the chain.

warning

Avoid excessive chaining as it can make your code harder for other developers to read.

Complete Coding ExampleLink to this section

Simulates a shopping cart applying discounts, shipping fees, and promotional multipliers.

This program demonstrates how multiple assignment operators work together to update a single variable step by step.

Important NotesLink to this section

  • Assignment operators store values into variables.
  • The basic assignment operator is the equals sign (=).
  • Compound assignment operators combine arithmetic and assignment.
  • Variables must be declared with let or var before updating them.
  • Constants declared with const cannot use assignment operators after declaration.
  • The expression on the right is always evaluated first.
  • Assignment operators evaluate from right to left.
  • += can also be used to concatenate strings.
  • Dividing by zero with /= produces Infinity.
  • Chaining assignments assigns the same value to multiple variables.

SummaryLink to this section

Assignment operators are essential tools for storing and updating data inside variables. By combining math operations with assignment, you can easily track scores, manage shopping carts, and modify application states. Mastering these operators prepares you to build interactive and dynamic JavaScript programs.

Check Your UnderstandingLink to this section

Javascript Assignment Operators quiz
Question 1 of 6

What is the basic assignment operator in JavaScript?

Practice ChallengesLink to this section

1. Update Game Score

easy

Create a variable named score starting at 50. Use the addition assignment operator to add 25 points, then print the final score.

Expected output

75

Hint: Use score += 25

2. Shopping Discount

easy

Create a variable named price with a value of 200. Use the subtraction assignment operator to apply a 50 discount, then print the price.

Expected output

150

Hint: Use price -= 50

3. Double the Money

medium

Create a variable named wallet with 30. Use the multiplication assignment operator to double the amount, then print it.

Expected output

60

Hint: Use wallet *= 2

4. Split the Bill

medium

Create a variable named total with 120. Use the division assignment operator to split it equally among 3 people, then print the result.

Expected output

40

Hint: Use total /= 3

5. Complex Score Tracker

hard

Start with a variable lives at 3. Subtract 1 life using -=, then multiply the remaining lives by 5 using *=, and print the result.

Expected output

10

Hint: Perform lives -= 1 first, then lives *= 5

Rate this content

Sign in to rate

Discussion

Sign in to join the discussion