DEV Community

JavaScript — Back to Basics: Prefix vs. Postfix

Himashi Hettege Dona on April 15, 2018

Wish me luck, I’m diving into JavaScript! As much as I want to start using JavaScript right away, and create applications, I know that...
Collapse
 
nestedsoftware profile image
Nested Software • Edited

As @AndrewBuntine said, this operator seems to originate from the world of C, or maybe even assembly language before that. I am not a big fan of it for general-purpose programming, and probably I would be inclined to suggest counter += 1 as a better idiom than counter++

Collapse
 
buntine profile image
Andrew Buntine

Nice article! This is thanks to C and has always been a gotcha in every language that implemented it.

Although, because of the semantic difference and the confusion it can cause, you will very rarely see auto-increment used inside other expressions in typical industry code - it would never pass a code review. You will see the postfix variant from time to time, but it's typically just run as a single statement on it's own line for the the side-effect (+ 1).

:)

Collapse
 
himashi99 profile image
Himashi Hettege Dona

@AndrewBuntine @nestedsoftware Thank you for the feedback. It's always interesting to hear how the topics I'm studying are used in the real world!