If you’re not familiar with the ++ sign in JavaScript, you might be wondering what does ++ means and how to use it.
In this article, we’ll explain in detail what ++ represents in JavaScript along with examples.
So, without further ado, let’s enhance your JavaScript skills.
What does ++ mean in JavaScript?
The two plus sign ++ is called the increment operator in JavaScript.
It increases the value of a variable by 1. There are two types of increment operators: the postfix increment operator (++) and the prefix increment operator (++).
Syntax
Prefix increment operator syntax: ++variable ✅
Postfix increment operator syntax: variable++ ✅Parameter
The increment operator ++ takes one operand, which must be a variable of a numeric data type.
Return value
The increment operator ++ returns a value based on its usage:
Prefix increment (++variable)It increments the value of the variable by 1 and returns the incremented value.
Postfix increment (variable++)It returns the original value of the variable and then increments the variable by 1.
Here’s an example:
let a = 10;
console.log(++a); // Output: 11 ✅
console.log(a); // Output: 11 ✅
let b = 15;
console.log(b++); // Output: 15 ✅
console.log(b); // Output: 16 ✅As you can see, the first console.log, “a” is incremented before it’s logged, so the output is 11.
In the second console.log, “b” is logged before it’s incremented, so the output is 15, but when we log “b” again, it has been incremented to 16.
How to use the increment (++) operator in JavaScript?
As we mentioned earlier, the increment (++) operator in JavaScript, allows you to increase the value of a variable by 1.
There are two ways to use the increment operator: the postfix increment operator (++) and the prefix increment operator (++).
Postfix Increment Operator
The postfix increment operator (++) is used after the variable name. It increments the variable value and returns the original value.
The value of the variable is incremented by 1, and the updated value is used in the expression.
Here’s an example:
let SampleNum = 10;
let result = SampleNum++; ✅
console.log(result);
console.log(SampleNum); In the above example, SampleNum++ increments the value of num by 1, but the returned value is the original value before the increment.
Output:
10
11Prefix Increment Operator
The prefix increment operator (++) is used before the variable name. It increments the variable value and returns the updated value.
Here’s an example:
let SampleNum = 15;
let result = ++SampleNum; ✅
console.log(result);
console.log(SampleNum);In our given example, ++num increments the value of num by 1, and the returned value is the updated value.
Output:
16
16Both the postfix and prefix increment operators are useful when you want to increase the value of a variable by 1.
However, it’s important to note that their behavior may differ when used as part of a larger expression or in different contexts.
Prefix vs Postfix — The Critical Difference
The placement of ++ matters when the increment is part of a larger expression. Compare:
let a = 5;
let b = a++; // postfix: b gets 5, then a becomes 6
console.log(a, b); // 6, 5
let c = 5;
let d = ++c; // prefix: c becomes 6, then d gets 6
console.log(c, d); // 6, 6The variable is updated to 6 in both cases. The difference is what the EXPRESSION evaluates to: postfix returns the OLD value (5), prefix returns the NEW value (6). This matters in loops, function arguments, and complex expressions.
++ vs += 1 — When to Use Which
| Pattern | Best For | Notes |
|---|---|---|
i++; (standalone) | Simple counter increment | Prefix/postfix produces same result here |
let x = ++i; | Need the NEW value | x and i are both equal to the incremented value |
let x = i++; | Need the OLD value before increment | Common in array indexing patterns |
i += 5; | Add any other number | More flexible than ++ |
i += 1; | Style preference (Airbnb style guide) | Some teams ban ++ entirely |
The popular Airbnb JavaScript Style Guide actually recommends NEVER using ++, preferring i += 1 for clarity. The reason: ++ can be missed when scanning code, especially when chained or buried in expressions. For BSIT capstone code, either style is acceptable as long as it is consistent.
Common Patterns Where ++ Shows Up
1. Classic for Loop
for (let i = 0; i < 5; i++) {
console.log(i); // 0, 1, 2, 3, 4
}2. Counter Increment Inside a Callback
let count = 0;
const numbers = [1, 2, 3, 4, 5];
numbers.forEach(n => {
if (n > 2) count++;
});
console.log(count); // 33. Array Index Increment
const items = [];
let i = 0;
items[i++] = "first"; // items[0] = "first", then i becomes 1
items[i++] = "second"; // items[1] = "second", then i becomes 2
console.log(items); // ["first", "second"]Common ++ Mistakes
- Using ++ on a const.
const i = 0; i++;throws TypeError. Constants cannot be reassigned. Useletfor any variable you increment. - Mistaking ++ for the increment of strings.
"5"++is not valid syntax. JavaScript coerces strings to numbers for ++, but the variable must be reassignable.let x = "5"; x++;works because x becomes the number 6. - Confusing ++a with a + 1.
a + 1creates a new value without modifying a.++achanges a in place AND returns the new value. Use the first for math, the second for counters. - Reading code with chained ++.
arr[i++] = arr[++j] + a++is technically valid but unreadable. Split into multiple statements. - Expecting ++ to be atomic. Single-threaded JavaScript makes this not matter, but in shared workers or atomics, ++ is NOT atomic. Use
Atomics.add()for shared memory counters.
Conclusion
In this article, we’ve explored the ++ sign in JavaScript, which is the increment operator in JavaScript, that is used to increase the value of a variable by 1.
You’ve learned that there are two types of increment operators: the prefix (++variable) and postfix (variable++) increment operators.
The prefix increment operator increments the value of the variable by 1 and returns the incremented value. On the other hand, the postfix increment operator returns the original value of the variable and then increments it by 1.
We hope this article has provided you with enough information to understand what does ++ mean in JavaScript.
Frequently Asked Questions
What does ++ mean in JavaScript?
The ++ operator increments a variable by 1. let i = 5; i++; sets i to 6. It works on numbers and on strings that can be coerced to numbers. The variable must be reassignable (declared with let or var, not const). The operator can appear either BEFORE the variable (prefix: ++i) or AFTER (postfix: i++), which affects what the expression returns.
What’s the difference between i++ and ++i in JavaScript?
Both increment i by 1, but they differ in what the expression EVALUATES to. i++ (postfix) returns the OLD value of i before incrementing. ++i (prefix) increments first, then returns the NEW value. The difference matters when the increment is part of a larger expression like let x = i++ vs let x = ++i.
Can I use ++ on a const variable?
No — const variables cannot be reassigned, and ++ reassigns the variable to its new value. const i = 0; i++; throws TypeError. Use let for any variable you plan to increment.
Should I use ++ or += 1 in modern JavaScript?
Both work. The Airbnb JavaScript Style Guide and many ESLint configs prefer i += 1 because it is more explicit and harder to miss in code review. For simple loops, i++ is universally understood. For BSIT capstone code, either is fine as long as you stay consistent within a single project.
What does ++ do to a string in JavaScript?
It coerces the string to a number first, then increments. let x = "5"; x++; console.log(x); outputs 6 (now a number). If the string is not numeric like let x = "abc"; x++;, the result is NaN. Generally avoid using ++ on string variables — use explicit conversion with Number(x) first for clarity.
Related JavaScript Tutorials
- JavaScript Math.round Function
- JavaScript Array find() Method
- JavaScript Date Add Days
- Reverse a String in JavaScript
- JavaScript Tutorial Series
Thank you for reading Itsourcecoders 😊.
