## DEV Community 👩‍💻👨‍💻 is a community of 921,001 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# What's the value of x?

Hey, devs from all over the world. 😊

``````let x = 0;

x++;
x = x++;
x = ++x;

console.log(x);
``````

What's the value of `x`? 🤔🤔

## Top comments (5) Gabriel Romualdo • Edited on

Placed before a variable, "++" adds one to the variable and the expression with "++" equals the new value of the variable + 1. This can be seen in line 4, where "x = ++x;" really means "x = (x + 1)".

Placed after a variable, however, "++" adds one to the variable and the expression with "++" equals the first value of the variable, without 1 added. This is why line 3, "x = x++;" has no effect — because the "x = x + 1" execution that the "++" gives is executed but then replaced by the the previous value of x.

In the end, you have:

``````let x = 0; // x = 0

x++; // x = x + 1 --> x = 1
x = x++; // x = x --> x = 1
x = ++x; // x = x + 1 --> x = 2

console.log(x); // 2
``````

I hope my explanation made sense to you, was a bit hard to explain.

— Gabriel