New course! Every coder should learn Generative AI!
Try a free lesson0
Increment and Decrement in JavaScript
Hey guys drop your own Explaination of increment and decrement in JavaScript! Let's help people who have problems with increment and decrement in JavaScript
2 Answers
+ 4
Kevin Neema Brian it's nice that you want to educate, but the Q&A section is meant to help the creator of a question to solve his/her problems with a specific topic. Your post belongs in your personal feed.
That said I'm sorry to tell you that your explanation is wrong.
b = a++ or b = ++a both consist of an assignment to b and an increment of a. Pre/post increment specified the order of those operations.
Pre (b=++a): a is increased first, then the incremented value of a is assigned to b
Post (b=a++): the value of a is assigned to b, and after that a is incremented
In all cases only a is incremented
+ 1
PRE increment: add 1 BEFORE evaluation in expression
POST increment: add 1 AFTER evaluation in expression
var pre = 0, post = 0;
console.log(++pre + pre); // 2 (1+1)
console.log(post++ + post); // 1 (0+1)