DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,673 amazing developers

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

Create account Log in
Raj Maharjan
Raj Maharjan

Posted on

Difference between i++ and ++i

Both prefix and postfix operators are used to mutate and update values. And both i++ and ++i alone does the same thing, increment the value of i by 1. This can sometimes be confusing for some beginners.

The difference can be found when assigned to other variables. For example:

let i = 0;
let j = i++; // first assigns to j then increments i by 1
console.log(j); // 0
let k = ++i; // first increments i by 1 then assigns to k
console.log(k); // 2
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.