Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Prefix and postfix operators

let a = 70;
a++; // This returns the value before incrementing.
console.log(a) // returns 71

Can someone explain to me why this doesn’t return 70, please?

let b = 70;
++b; // Using it as a prefix returns the value after incrementing.
console.log(b) // returns 71

I understand how prefix works.

I have read explanations and watched videos on this subject but it’s still confusing to me. Thank You.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Both versions of them increment the value they’re applied to. If you do nothing with the value of the increment expression itself, they’re equivalent, both of them doing the same thing as i += 1. The only time it matters is when you use the result of the increment expression:

let a = 70;
console.log(a++); // outputs 70, the old value of a, but a will be 71 afterwards
console.log(a)    // outputs 71; no matter how you increment, after the increment, the value has changed

let b = 70;
console.log(++b); // outputs 71; the new value of b is produced
console.log(b)    // outputs 71; same as in case a
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading