JAVASCRIPT

JavaScript ++ Operator: Syntax, Usage, and Examples

The ++ operator in JavaScript is known as the increment operator. It increases a variable’s value by 1 and is frequently used in counters, loops, and event tracking.

How to Use the ++ Operator in JavaScript

The ++ operator can be written in two ways:

  • Post-increment: variable++
  • Pre-increment: ++variable

Post-increment

In post-increment, the variable’s original value is used first, and then it’s incremented.

let likes = 5;
let result = likes++;
console.log(result); // 5
console.log(likes);  // 6

Pre-increment

In pre-increment, the variable is increased before being used in an expression.

let likes = 5;
let result = ++likes;
console.log(result); // 6
console.log(likes);  // 6

Both increase the value by 1, but they return different results depending on whether the update happens before or after the value is used.

When to Use the ++ Operator in JavaScript

The JavaScript plus plus operator is most useful in places where you need to increase a number by one without typing the longer form x = x + 1 or x += 1.

Here are common situations where the ++ operator is helpful:

Loop counters

The ++ operator is often used to increment the loop variable in a for loop.

for (let i = 0; i < 5; i++) {
  console.log("Step", i);
}

Using ++ inside the loop structure is a standard approach and keeps the syntax clean.

User interaction tracking

Whether it's counting button clicks, form submissions, or menu selections, you can easily update values.

let clickCount = 0;
button.addEventListener("click", () => {
  clickCount++;
  console.log(`Clicked ${clickCount} times`);
});

Game or app scoring

Tracking a player's score or points in an app? The increment operator is your friend.

let score = 10;
score++;
console.log("New score:", score);

Examples of ++ in JavaScript

Here are more examples of how to use the ++ operator in practical situations:

Example 1: Pre-increment inside a calculation

let level = 2;
let nextLevel = ++level * 10;
console.log(nextLevel); // 30

The variable level is first incremented to 3, then multiplied by 10.

Example 2: Post-increment inside a calculation

let stage = 2;
let bonus = stage++ * 10;
console.log(bonus); // 20
console.log(stage); // 3

Here, the original value is used in the multiplication, then increased afterward.

Example 3: Inside a while loop

let counter = 0;
while (counter < 3) {
  console.log("Counter is", counter);
  counter++;
}

This logs 0, 1, and 2 before stopping.

Learn More About the ++ Operator in JavaScript

Differences between post-increment and pre-increment

Though both forms increase the value by 1, the position of the operator changes the order in which the value is returned.

Post-increment (x++) uses the current value, then increments.

Pre-increment (++x) increments first, then uses the new value.

This matters most when you're assigning or returning values inside expressions.

let x = 5;
let a = x++; // a = 5, x = 6

let y = 5;
let b = ++y; // y = 6, b = 6

Avoid confusing chains

You might see code like this:

let a = 1;
let b = a++ + a++;

It’s legal, but hard to read and understand. The result might surprise you.

Break such expressions into smaller steps. It’s easier to debug and makes your intent clear to others (and your future self).

Using ++ with object properties

You can increment object properties directly:

let stats = { likes: 0 };
stats.likes++;
console.log(stats.likes); // 1

This is especially useful when dealing with analytics or counters stored in objects.

Incrementing array elements

You can also use the operator to increment array values:

let numbers = [1, 2, 3];
numbers[0]++;
console.log(numbers); // [2, 2, 3]

It works just like with regular variables.

Comparison with += 1

The two are functionally the same:

let count = 4;
count++;

...is the same as:

let count = 4;
count += 1;

However, the plus equal operator is more versatile if you're adding numbers other than 1.

count += 5; // Adds 5

So use ++ when you’re only incrementing by 1 and want a quick, readable approach. Use += when you need more flexibility or consistency across your codebase.

++ operator inside functions

When used inside a function, the increment doesn’t affect the outer variable unless it’s reassigned.

function increase(num) {
  return ++num;
}

let score = 7;
let newScore = increase(score);

console.log(score);    // 7
console.log(newScore); // 8

This is because primitive values like numbers are passed by value, not by reference.

Common mistakes with ++

  • Forgetting the return order: Be cautious when using x++ vs ++x in complex expressions.
  • Overusing in expressions: Don’t try to increment multiple times in a single statement unless absolutely necessary.
  • Using it with constants: const variables cannot be incremented. This will throw an error:

const max = 10;
max++; // ❌ TypeError

Use let if the value will change.

The ++ operator in JavaScript is a shorthand for adding 1 to a variable. It’s compact, useful, and commonly used in loops, counters, and user interaction logic. Understanding the difference between post-increment (x++) and pre-increment (++x) is key to writing predictable, readable code.

If you’re increasing values one step at a time, the JavaScript plus plus operator keeps your code clean and efficient. Just remember: with great power comes the occasional off-by-one error — use it wisely.

Learn to Code in JavaScript for Free
Start learning now
button icon
To advance beyond this tutorial and learn JavaScript by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2025 Mimo GmbH

Reach your coding goals faster