+ 1

Why y equals 4 here instead of 3?

var x = 1 y = ++x + x alert(y) I assume "++x" changed the value of x first and thus by adding it to x (which is 'itself') the equation becomes like "y = 2 + 2", am I correct?

18th Jan 2022, 8:05 PM
Theoz
Theoz - avatar
1 Resposta
+ 4
Yes. Your reasoning is correct. If you reverse the order then you will get 3. y = x + ++x
18th Jan 2022, 8:14 PM
Brian
Brian - avatar