0

I'm examining some example javascript code and came upon something like the following:

a=b=c+1

I've never really coded in javascript before, so I'm not familiar with the order of evaluation...My gut tells me that the above would evaluate as a=b THEN b=c+1...so if b were initially 1 and c=2 then following that line of code a=1 and b=3

Is this correct, or is the evaluation order opposite, i.e. b=c+1 THEN a=b, in which case a=3 and b=3?

DrCocoa
  • 37
  • 1
  • 10
  • 1
    `a` equals `b`. `b` is an *expression*, so `b` has to be calculated. `b` equals `c + 1`. `c` gets looked up and 1 is added to it, result is assigned to `b` and value of `b` is assigned to `a`. It's easier if you read it as `a = (b = c + 1);`. – Mjh Feb 24 '16 at 16:29
  • I hope my answer to the duplicate clears up all your concerns – Bergi Feb 24 '16 at 16:31
  • Why was this closed? – Sukima Feb 24 '16 at 16:32
  • @jcaron: No. Associativity is about nesting precedence in parsing, everything in js is *evaluated* left-to-right. – Bergi Feb 24 '16 at 16:32
  • Here is an answer since this was unexpectedly closed: https://gist.github.com/sukima/33367cbfc19eaa40ca43 – Sukima Feb 24 '16 at 16:34
  • Thank you all. @Sukima, I appreciate the follow-up clarification. – DrCocoa Feb 24 '16 at 16:40
  • @Bergi, indeed... Learned something new today, I thought they were evaluated in the order they are "used" and thus based on precedence and associativity, but it's definitely not the case. – jcaron Feb 24 '16 at 17:40

0 Answers0