Why y equals 4 here instead of 3? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 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 Réponse
+ 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