+ 2
Why is the output of this 7 and 7?
Shouldn't this increment in the first console log before displaying? I understand the second console log. https://code.sololearn.com/W9z0vpU16181/?ref=app
4 Respostas
+ 6
If you want to increment sum, use either sum++ or ++sum, not sum = ++sum. I don't think that there's something like undefined behavior in JavaScript, but in other languages an expression like x = x++ would be wrong and lead to undefined/unpredictable results. Even if that's not the case in JS, I'd say it's not good programming style because you use the increment operators in a way they were not intended to
+ 5
Anna Though i agree with you, i think than the code is only for make thinking about the operator in not-usual case... Like a challenge. I dont think than a similar code is useful for others reasons
+ 4
var sum=5
sum= sum++
What will be sum at this point? 6? Wrong. It will be equals to 5.
This because sum++ return the value of sum BEFORE than its been incremented and, though incrementation happen, it happen before than "returned" value is assigned to sum (with assignement operator) making increments really ineffective.
Now :
sum = ++sum;
Here ++sum will return the value AFTER the increment and sum will be 6
console.log(++sum);
At this point, sum==6 and for same motivations than you seen before, 7 will be printed abd sum will be
console.log(sum++);
Here will be printed 7 just because what i said before, but note than after this line sum is 8
+ 3
EveryThingIsEasy() yes but you missed the increment operations within console.log(). Also KrOW is saying that the increment has no effect in sum=sum++; This was new to me. It's as if two variables are used behind the scenes here and not a single one called sum.