DEV Community

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)