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.
| Operator | Name | Example | Same As |
|---|---|---|---|
= | Assignment | x = y | x = y |
+= | Addition assignment | x += y | x = x + y |
-= | Subtraction assignment | x -= y | x = 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
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
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
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
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
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
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
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
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
What is the basic assignment operator in JavaScript?
Practice ChallengesLink to this section
1. Update Game Score
Create a variable named score starting at 50. Use the addition assignment operator to add 25 points, then print the final score.
Expected output
75Hint: Use score += 25
2. Shopping Discount
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
150Hint: Use price -= 50
3. Double the Money
Create a variable named wallet with 30. Use the multiplication assignment operator to double the amount, then print it.
Expected output
60Hint: Use wallet *= 2
4. Split the Bill
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
40Hint: Use total /= 3
5. Complex Score Tracker
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
10Hint: Perform lives -= 1 first, then lives *= 5
Rate this content
Sign in to rate
Discussion
Sign in to join the discussion