Understanding Post-Increment (a++) and Pre-Increment (++b) in JavaScript

Understanding Post-Increment (a++) and Pre-Increment (++b) in JavaScript
Photo by Kyle Glenn / Unsplash

When you're new to programming, one of the first things you learn is how to work with variables and operators. JavaScript, like many other languages, offers various ways to manipulate variables. Two commonly used operations that often confuse beginners are post-increment (a++) and pre-increment (++a). These terms refer to how numbers are increased by 1. In this article, we'll break down these concepts using a simple example to make sure novice programmers understand how it all works.

A Simple Example

Let's start by looking at this piece of JavaScript code:

let a = 10;
let b = 5;
let c = a++ + ++b;

console.log(c); // What will c be? OK, the answer is 16 but the process to get there maybe not like you think

This may seem confusing at first, especially with the a++ and ++b. However, once we break it down, it will be much clearer.

The Basics: Incrementing Variables

In programming, incrementing means increasing a value by 1. For example:

let x = 5;
x++; // x is now 6

This code adds 1 to the variable x. Easy enough, right? But what does the ++ mean when it appears before or after a variable? That’s where it gets interesting!

Post-Increment (a++) vs. Pre-Increment (++a)

  • Post-Increment (a++): The current value of the variable is used first, and then the variable is incremented.
  • Pre-Increment (++a): The variable is incremented first, and then the new value is used.

Let’s explain this in a step-by-step way.

Step-by-Step Breakdown

Setting initial values:

let a = 10;
let b = 5;

At this point:

  • a = 10
  • b = 5

Evaluating the expression: c = a++ + ++b

  • a++ (Post-Increment):The current value of a (which is 10) is used in the expression.After using the current value, a is incremented. So, a becomes 11, but this new value isn't used in the calculation right now—only 10 is used.
  • ++b (Pre-Increment):The value of b is incremented first, so b becomes 6.Then, this new value (6) is used in the calculation.

Performing the calculation: The expression becomes:

c = 10 + 6; // which equals 16

Result: After this line of code runs:

  • a = 11 (because of the post-increment, a++)
  • b = 6 (because of the pre-increment, ++b)
  • c = 16 (the result of the calculation)

Visualizing Post-Increment vs. Pre-Increment

Here’s a way to visualize the difference between post-increment and pre-increment:

  • Post-Increment (a++):
    • Use the current value of a, then increment it.
    • Imagine a is holding up a sign that says "10". You use that "10" for whatever you need, but then behind the scenes, a changes its sign to "11". However, you don’t see this new value yet; it will only appear in the next line of code.
  • Pre-Increment (++b):
    • Increment first, then use the new value.
    • Imagine b is holding a sign that says "5". Before you use it, b changes the sign to "6", and then you use this updated value.

Practical Example

Let’s take a real-world example to understand this better:

Imagine you are at a counter buying apples. There are two different types of apple counters:

  1. Post-Increment Counter:
    • You take an apple from the counter, and it shows you how many apples it had before you took one (10 apples).
    • After you take the apple, the counter updates to show 9 apples, but you already took yours based on the previous count.
  2. Pre-Increment Counter:
    • Before you take an apple, the counter updates itself to show one less apple (6 apples).
    • You take the apple based on the new, updated count.

Common Mistake for Beginners

One common mistake for beginners is thinking that a++ immediately increments a before it’s used. As we’ve seen, this isn’t the case. In fact, post-increment waits until after the value has been used before increasing the variable.

Here’s an incorrect assumption beginners might make:

let a = 10;
let b = a++;
console.log(b); // They might expect b to be 11, but it's actually 10

Why is b equal to 10 and not 11? This is because the value of a was used before it was incremented, so b is assigned 10, and then a is incremented to 11 afterward.


Remember This

  • Post-Increment (a++): Use the current value first, then increment afterward.
  • Pre-Increment (++a): Increment the value first, then use the new value.
  • Order matters! When you see a++ and ++b in an expression, remember that the position of ++ (before or after) changes when the increment happens in relation to the expression.

Now You Should Understand

Understanding the difference between post-increment (a++) and pre-increment (++b) is essential for working effectively with JavaScript. By following the step-by-step breakdown in this article, you should now have a clearer picture of how these operators work and how they can affect your calculations.

Common mistakes is natural, but as you continue learning JavaScript it will help you to avoid common mistakes and build a strong foundation for more advanced programming techniques.

Keep make mistakes, keep learning.

Support Us

Subscribe to Buka Corner

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe