Why is the variable b incremented? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is the variable b incremented?

It’s javascript var a = 2 var b = 3 var z = ++b alert(b)

12th Jan 2018, 2:11 AM
Semen Maltsev
Semen Maltsev - avatar
2 Answers
+ 2
because your're first incrementing the value of b, and then setting it to z. so, both b and z will be 4. if you want b unchanged, do var z = b + 1
12th Jan 2018, 2:52 AM
Salekin
Salekin - avatar
0
Or use post incrementation instead of pre incrementation to slighlty modify the behaviour: var b = 3; var z = b++; alert(b); // 4 alert(z); // 3
12th Jan 2018, 5:18 AM
visph
visph - avatar