0

I have this simple code on the JavaScript.

var a = 10;
var b = 20;

a=a+b-(b=a);

document.write("a = " + a + "</br> b = " + b);

Can somebody explain me, how did these variables change the values and how is the assignment operator works in this case ? I think, that on the first step the variable b is rewrote by number from a: (b=a).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Alexander
  • 93
  • 8
  • 1
    What specifically are you asking about? Have you looked up how assignment operators work? Is JS your first language, or do you have knowledge of other languages? – Carcigenicate Aug 22 '17 at 21:55
  • Assignment operator returns the assigned value, so `b=a` returns `10`. – Ram Aug 22 '17 at 22:00
  • a is getting assigned the value of a + b minus the new value of b that was just assigned the value of a... I think that answer is equally as clear as the question ;) – TinMonkey Aug 22 '17 at 22:02
  • I believe MDN's page on Operator Precedence contains all the information needed to figure this one out: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence – shabs Aug 22 '17 at 22:05
  • @shabs The parenthesis make the precedence clear already. What the page doesn't mention is evaluation order. – Bergi Aug 22 '17 at 22:10
  • @Bergi the first sentence of the page is "Operator precedence determines the order in which operators are evaluated." -- can you help me understand what im missing? – shabs Aug 22 '17 at 22:12
  • @shabs Oh, that sentence is wrong. Operator precedence is about parsing into a syntax tree and which is the operand of which, not about evaluation order (though an *arithmetic* operator has to evaluate all its operands before being processed itself, of course). I'll fix MDN. – Bergi Aug 22 '17 at 22:17
  • [ -- deleted -- ] – shabs Aug 22 '17 at 22:29
  • My suggestion is to never write code like this. It won't pass the linter. – RandyTek Aug 22 '17 at 22:55

3 Answers3

2

It's evaluated outside-in, from left to right, as usually.
The assignment expression returns the assigned value.

a = a + b - (b = a);    // a=10 b=20
a = 10 + b -( b = a);   // a=10 b=20
a = 10 + 20 - (b = a);  // a=10 b=20
a = 30 - (b = a);       // a=10 b=20
a = 30 - (b = 10);      // a=10 b=20
a = 30 - (10);          // a=10 b=10
a = 30 - 10;            // a=10 b=10
a = 20;                 // a=10 b=10
20;                     // a=20 b=10
Ivan
  • 34,531
  • 8
  • 55
  • 100
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • `a=10+20-(b= a); // a=10 b=20` `a= 30 - (b= a); // a=10 b=20` I thought that the first expression will be evaluated in brackets, but in this example, the first step is `10+20`. – Alexander Aug 22 '17 at 22:16
  • @Alexander Yes, that was exactly your misconception. If `(b=a)` would have been evaluated before `a+b`, you wouldn't get the result you're actually getting. – Bergi Aug 22 '17 at 22:27
  • @Alexander The first expression that is evaluated is the outer assignment, which then has to evaluate the `-` operation, which then has to evaluate its left operand (the sum) and after that its right operand (the inner assignment). Outside-in, left-to-right. – Bergi Aug 22 '17 at 22:29
1

Simple explanations below.

1 . We are assigning our initial values:

var a = 10;
var b = 20;

2 . Here we're saying a is equal to 10+20 - (10). Therefore a is now equal to 20 and b is equal to 10 as it was assigned to be a before we assigned a's new value.

a=a+b-(b=a);

3 . Result:

var a = 10;
var b = 20;

a = a + b - (b = a);

console.log("a = " + a); // a = 20
console.log("b = " + b); // b = 10
Ivan
  • 34,531
  • 8
  • 55
  • 100
Christopher Messer
  • 2,040
  • 9
  • 13
0

Well, let's look closely at this: a = a + b - (b = a);

Let's replace variables with values a = (10 + 20) - (10)

This is because B == 20 until redefined at the end of the expression.

SkiZer0
  • 21
  • 7