- -- operator
- -= operator
- ++ operator
- += operator
- Accessing and setting content
- Array concat() method
- Array indexOf()
- Array length
- Array pop()
- Array shift
- Arrays
- Booleans
- Braces
- Callback function
- Calling the function
- Class
- Closure
- Code block
- Comment
- Conditions
- Console
- Constructor
- Creating a p element
- Data types
- Date getTime()
- Destructuring
- Else
- Else if
- Enum
- Equals operator
- Error Handling
- ES6
- Event loop
- Events
- Extend
- Fetch API
- Filter
- For loop
- forEach()
- Function
- Function bind()
- Function name
- Greater than
- Head element
- Hoisting
- If statement
- includes()
- Infinity property
- Iterator
- JSON
- Less than
- Local storage
- Map
- Methods
- Module
- Numbers
- Object.keys()
- Overriding methods
- Parameters
- Promises
- Random
- Reduce
- Regular expressions
- Removing an element
- Replace
- Scope
- Session storage
- Sort
- Splice
- String
- String concat()
- String indexOf()
- Substring
- Switch statement
- Template literals
- Ternary operator
- Title
- Type conversion
- While loop
JAVASCRIPT
JavaScript += Operator: Syntax, Usage, and Examples
The +=
operator in JavaScript adds a value to a variable and then updates that variable with the result. Known as the plus equal operator, this shorthand saves you from repeating the variable name when performing addition.
How to Use the += Operator in JavaScript
Using the +=
operator is straightforward. Instead of writing x = x + y
, you can write x += y
. Here’s a simple example:
let likes = 5;
likes += 1; // adds 1 to likes
console.log(likes); // 6
This operator works with numbers, strings, and even expressions:
let score = 10;
let bonus = 5;
score += bonus; // score is now 15
let message = "Hello";
message += " world!";
console.log(message); // Hello world!
The +=
operator performs addition for numbers and concatenation for strings.
When to Use the += Operator in JavaScript
The += operator in JavaScript is used often in everyday coding tasks. Here are a few scenarios where it makes your code cleaner and easier to follow:
1. Tracking Counts or Scores
In games, forms, or analytics tools, +=
helps you increase counters.
let totalPoints = 0;
totalPoints += 10; // added points
2. Building Strings Dynamically
When generating strings line by line or word by word, +=
simplifies the process.
let bio = "Name: Alice\n";
bio += "Age: 28\n";
bio += "Location: Berlin";
console.log(bio);
3. Loop Accumulations
In loops, the +=
operator can accumulate totals or build values efficiently.
let sum = 0;
for (let i = 1; i <= 5; i++) {
sum += i;
}
console.log(sum); // 15
Examples of += in JavaScript
Example 1: Adding Numbers
let distance = 100;
distance += 50;
console.log(distance); // 150
This adds 50 to the existing value of distance
.
Example 2: Concatenating Strings
let announcement = "Attention: ";
announcement += "Meeting starts at 2 PM.";
console.log(announcement); // Attention: Meeting starts at 2 PM.
When applied to strings, +=
appends text.
Example 3: Using += with Arrays (via Loops)
let numbers = [1, 2, 3, 4, 5];
let total = 0;
for (let num of numbers) {
total += num;
}
console.log(total); // 15
This adds each element in the array to a total variable using the plus equal operator.
Learn More About the += Operator in JavaScript
How It Compares to ++
The ++
operator increments a variable by exactly one. In contrast, the JavaScript plus equal operator lets you add any value.
let count = 3;
count++; // adds 1
count += 4; // adds 4
So, if you always want to add just one, go for ++
. If you want more control over the value, stick with +=
.
String Coercion with +=
JavaScript is loosely typed, so when using +=
, you may end up with a string even when working with numbers. That’s because if one of the values is a string, JavaScript converts the other to a string and performs concatenation.
let output = "Result: ";
let value = 100;
output += value;
console.log(output); // "Result: 100"
But:
let value = "100";
value += 50;
console.log(value); // "10050"
This happens because the number 50 gets coerced into a string.
To avoid issues, always check the types:
let value = parseInt("100", 10);
value += 50;
console.log(value); // 150
+= with Objects and Properties
You can also use the += operator with object properties:
let player = {
score: 0
};
player.score += 20;
console.log(player.score); // 20
This is very common in stateful apps or games where values are tied to objects.
+= in Loops for Building Output
let stars = "";
for (let i = 0; i < 5; i++) {
stars += "*";
}
console.log(stars); // *****
Here, the string grows in every iteration of the loop. This is a simple but clear use of +=
in dynamic content generation.
+= with Functions
Functions that return values can also work with the +=
operator:
function getTip(amount) {
return amount * 0.15;
}
let totalBill = 100;
totalBill += getTip(totalBill); // Adds 15% tip
console.log(totalBill); // 115
The += operator in JavaScript is a compact way to update a variable by adding to its existing value. Whether you're tracking scores, building strings, or summing arrays, it helps reduce repetition and keeps your code tidy.
Remember: use it with numbers to add, with strings to concatenate, and always be aware of JavaScript's type coercion to avoid surprises. The JavaScript plus equal operator is a small piece of syntax with big utility across all kinds of projects.
Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.