Incrementing. If else statement. JavaScript basic

What is the difference between count++ and count+=1?

both are generally same

for example

let a = 10;
a++;
console.log(a);
//output = 11

let b = 10 ;
b +=1;
console.log(b);
//output = 11

Similar, but not quite the same. Here’s an example:

let count = 1;
// why you'd do this, I don't know
const latestCount = count += 1; 
console.log(`latestCount is ${latestCount}, while count is ${count}`);
// "lastCount is 2, while count is 2"

// As opposed to:
const anotherOne = count++;
console.log(`anotherOne is ${anotherOne}, while count is ${count}`);
// "anotherOne is 2, while count is 3"

you see, the first one does the increment and then evaluates count in order to assign the value. But the second is the “postfix incrementor”, so it returns count before it does the incrementing.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.