+ 2

Can someone really explain what's going on here?

Why does the first one alert 6 and the next, 5? let a = 3; let b = a++; let c = ++a; alert(++c); // 6 let a = 3; let b = a++; let c = ++b; alert(++c); // 5

30th Jun 2019, 6:12 AM
Jesse Brite
Jesse Brite - avatar
2 Answers
+ 2
You hopefully know the difference between prefix and postfix. Let's run through it: let a = 3; let b = a++; //a = 4, b = 3 let c = ++a; //a = 5, c = 5 alert(++c); //c = 6, alerted 6 let a = 4; let b = a++; //a = 4, b = 3 let c = ++b //b = 4, c = 4 alert(++c); //c = 5, alerted 5
30th Jun 2019, 6:16 AM
Airree
Airree - avatar
+ 2
Thanks for the clarity. I get it now
30th Jun 2019, 6:22 AM
Jesse Brite
Jesse Brite - avatar