1
var a=1;
b=++a*a;
console.log(b);
console.log(a);

the result is 4,2.how the program get this result? in my mind,the result will be 2,2

can anybody tell me how the javascript compiler compile this piece of code and get the result 4,2.

Then the deep question is why these two pieces of code result are the same.

var a=2;
var b=3;
c=(a++)*a; 
console.log(c);


var a=2;
var b=3;
c=(a++)*b; 
console.log(c);

can anyone explain this one step by step?

rici
  • 234,347
  • 28
  • 237
  • 341
JackieWillen
  • 673
  • 1
  • 12
  • 23
  • why this will get error in c programming language.Like this int a=1; b=++a*a; console.log(b); console.log(a); what is the difference between c and javascript,why c get errors. – JackieWillen Jul 14 '17 at 07:50
  • The difference between Javascript and C (other than the obvious syntactic differences) is that Javascript always evaluates right-to-left and C doesn't; C can evaluation the arguments to `*` in either order, or even in parallel. As a result, you are not allowed to modify and use a variable in the same expression. See https://stackoverflow.com/questions/949433/why-are-these-constructs-using-undefined-behavior – rici Jul 14 '17 at 16:41

2 Answers2

1

++ has a higher precedence than *. Thus b = ++ a * a is evaluated as b = (++a) * a.

++a makes a equal to 2 and then a gets muliplied by itself.

On a sidenote, every time you get confused by something like this, find JavaScript's operator precedence table and try to break the equation down by yourself.

Kapol
  • 6,383
  • 3
  • 21
  • 46
  • why this will get error in c programming language.Like this int a=1; b=++a*a; console.log(b); console.log(a); – JackieWillen Jul 14 '17 at 07:48
  • 1
    I'm no C expert, but it's probably because `b` is not declared. Try `int b = ++a*a;` – Kapol Jul 14 '17 at 07:57
  • i add int before b,but still get errors like: Line 2: error: initializer element is not constant Line 3: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token Line 4: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token – JackieWillen Jul 14 '17 at 08:03
  • sorry,the c language has no console command,let me change this to print – JackieWillen Jul 14 '17 at 08:05
  • I have tried,you are right.the result is same.Error is just because of syntax error .Thank you very much – JackieWillen Jul 14 '17 at 08:08
  • This error message makes me want to split the declaration into `int b = 0; b = ++a*a`. You're welcome. – Kapol Jul 14 '17 at 08:08
  • @kapol: In C, that's undefined behaviour because C does not define the moment in which the value of `a` is actually incremented, nor does it define the order of evaluation of the arguments to `*`. This is well-described in any number of SO questions about C sequence points. – rici Jul 14 '17 at 16:08
  • @rici Thanks for the explanation – Kapol Jul 14 '17 at 16:24
0

++a increases the value to 2 before the multiplication. After this, variable "a" will point to value 2 and it will make the multiplication: 2*2.

a++*a wil give you the desired result (2,2)

Alex
  • 166
  • 8
  • why this will get error in c programming language.Like this int a=1; b=++a*a; console.log(b); console.log(a); what is the difference between c and javascript,why c get errors. – JackieWillen Jul 14 '17 at 07:49