It's evaluated outside-in, from left to right, as usually.
The assignment expression returns the assigned value.
Copya = 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
Answer from Bergi on Stack OverflowThe assignment operator in JavaScript - Stack Overflow
How do i use an assignment operator
JavaScript OR (||) variable assignment explanation - Stack Overflow
Logical assignment operators in JavaScript
Videos
It's evaluated outside-in, from left to right, as usually.
The assignment expression returns the assigned value.
Copya = 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
Simple explanations below.
1 . We are assigning our initial values:
Copyvar 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.
Copya=a+b-(b=a);
3 . Result:
Copyvar a = 10;
var b = 20;
a = a + b - (b = a);
console.log("a = " + a); // a = 20
console.log("b = " + b); // b = 10
Run code snippetEdit code snippet Hide Results Copy to answer Expand
See short-circuit evaluation for the explanation. It's a common way of implementing these operators; it is not unique to JavaScript.
This is made to assign a default value, in this case the value of y, if the x variable is falsy.
The boolean operators in JavaScript can return an operand, and not always a boolean result as in other languages.
The Logical OR operator (||) returns the value of its second operand, if the first one is falsy, otherwise the value of the first operand is returned.
For example:
"foo" || "bar"; // returns "foo"
false || "bar"; // returns "bar"
Falsy values are those who coerce to false when used in boolean context, and they are 0, null, undefined, an empty string, NaN and of course false.