+ 5

# I'm not quite understand, why the result is 23 not 25. Is the parenthesis here should be ignored?

var a=10; var k; a++; k=(a++)+(a++); document.write(k);

8 Antworten

+ 3

11+12=23

+ 2

it first assign the value then increment value. i.e. first a=10 then a becomes 11 on a++ then
k =(a++) here 11 is assign to a then a is increment by 1 i.e a=12 now. then (a++) here 12 is assign to a.
so the final answer is k = 11+12 =23

+ 2

thanks all for the explanations
I got it, here my understanding
var a=10; // a = 10
var k;
a++; // a = a + 1 , this is mean a now 11
k=(a++)+(a++); // k=(a=a+1)+(a=a+1) here the challenge, i explain it below
document.write(k);
------
k=(a=a+1)+(a=a+1)
first, k=a which is a from previous value and before increased (a still 11), so k = 11
second, still in the first (a=a+1), a then increased by 1, so a become 12
third, a after become 12 is assigned to a in the second (a=a+1)
fourth, now the equation become k=11+12
a then incremented by 1 in the second (a=a+1)
but not include in k equation, because a value used in a++ is the a before incremented.
clear.
Oh ya, the final a value is 13. but not included in the k equation. Like i said before, the last a used for k equation is 12 (before incremented).
and my hunch is right. the parenthesis are not needed.

+ 1

jika mencoba ini hasilnya sama saja 23
var a=10;
var k;
a++;
k=(a++)+a;
document.write(k);

0

postincrement

0

pertama tama nilai a = 10
lalu a++ menjadi 11
lalu ditambahkan lagi (a++) nilanya berubah menjadi 12 tapi hanya satu saja
kesimpulan :
k=12+11
k=23

0

if you want the output is 25
try this :
var a=10;
var k;
a++;
k=(a++)+(a+=2);
document.write(k);

0

just easy as pie
{a=10}
a equals 11 then k =11
{k=a++}
now a=12 but k still 11
{a++}
now a=13 but k still 11
{k=a+++a++}
now a=13 but k=11+12=13