DEV Community

Raj Maharjan
Raj Maharjan

Posted on

1 2

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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay