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).

9
  • 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? Commented Aug 22, 2017 at 21:55
  • Assignment operator returns the assigned value, so b=a returns 10. Commented Aug 22, 2017 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 ;) Commented Aug 22, 2017 at 22:02
  • I believe MDN's page on Operator Precedence contains all the information needed to figure this one out: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Aug 22, 2017 at 22:05
  • @shabs The parenthesis make the precedence clear already. What the page doesn't mention is evaluation order. Commented Aug 22, 2017 at 22:10

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

3 Comments

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 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.
@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.
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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.