4

As each programming language is different and my experience with Javascript is on basic level, I would like to know, how multiple variable assignments in one row are evaluated

Example:

 a = b = c = d = 5;

Will such statement assign 5 to each of 4 variables a, b, c and d?

Thanks.

000
  • 26,951
  • 10
  • 71
  • 101
Ωmega
  • 42,614
  • 34
  • 134
  • 203

4 Answers4

16

The short answer is yes, that statement will assign 5 to each of 4 variables a, b, c and d. But, contrary to what was said, doesn't assign 5 to d, and then the value of d to c, but it will assign the same value to each variables, starting from the right-hand side. To be more clear, your statement:

var a, b, c, d;
a = b = c = d = 5;

It's equivalent to:

var d = 5;
var c = 5;
var b = 5;
var a = 5;

Not to:

var d = 5;
var c = d;
var b = c;
var a = b;

It's a subtle but important difference: in the first case, JavaScript just sets a value to all the variables. In the second case, JavaScript set a value to all the variables but also get the value of three variables (the value of a is not assigned anywhere).

A simple code that will show that:

// `this` is the global object if you run this code in the global scope.
// In the browsers the global object is `window`.
Object.defineProperties(this, {  
  "a": {  
    get: function() {
        console.log("get a");
    },
    set: function(value) {
        console.log("set a");
    }
  },  
  "b": {  
    get: function() {
        console.log("get b");
    },
    set: function(value) {
        console.log("set b");
    }
  },  
  "c": {  
    get: function() {
        console.log("get c");
    },
    set: function(value) {
        console.log("set c");
    }
  },  
  "d": {  
    get: function() {
        console.log("get d");
    },
    set: function(value) {
        console.log("set d");
    }
  }  
});

b = c = d = 5;
a = b;

On the console you should have:

set d
set c
set b
get b
set a

As you can see for the statement b = c = d = 5 JS only set the variable, and call both set and get on b, because the statement a = b.

This distinction is very important, because if you define some getter for your property and you're not aware of this behavior, you will end up with unexpected bug using multiple variable assignments.

ZER0
  • 24,846
  • 5
  • 51
  • 54
  • it's worth noting that the assignment expression returns the right value along with setting the left reference according to [the spec](http://www.ecma-international.org/ecma-262/5.1/#sec-11.13.1) – Roy Ling Mar 20 '16 at 13:33
  • Not sure what you mean, the [the spec](http://www.ecma-international.org/ecma-262/5.1/#sec-11.13.1) says exactly the same thing in fact; I just tried to elaborate with an example that shows the set / get behavior, compare to previous answers. – ZER0 Mar 21 '16 at 10:25
5

For the most part yes, but consider the following scenario:

Object.defineProperty(this, "b", { value : 6, writable : false });
var a, c, d;

a = b = c = d = 5;

console.log(a); // 5
console.log(b); // 6
console.log(c); // 5
console.log(d); // 5
jalbee
  • 951
  • 6
  • 10
  • Thanks. As expected - there is always some exception :) – Ωmega Jul 17 '12 at 00:27
  • good example of exception, and that's because the assignment `b = c` has a return value as 5 which is then assigned to `a`, but `b` is not writable actually (thus 6). – Roy Ling Mar 20 '16 at 13:34
2

The answer is yes.

a=b=c=d=e=f=g=h=4; // all get to be 4
a=b=c=d=e=f=g=h={ p1:"1", p2:"2" }; // same object for all

a.p3 = "3";
console.log((a==b && b==c && c==d && d==e), f); // condition is true, f is now 
//    {
//        p1:"1",
//        p2:"2",
//        p3:"3"
//    }
Silviu-Marian
  • 10,565
  • 6
  • 50
  • 72
0

You have only to try it to know the answer: Yes.

The reason it works like that is that assignment takes whatever is on the right-hand side of the equals sign and assigns that value to the variable on the left-hand side.

So 5 is assigned to d; then the value of d is assigned to c and so on.

Ωmega
  • 42,614
  • 34
  • 134
  • 203
Andrew Leach
  • 12,945
  • 1
  • 40
  • 47
  • No, it is NOT assigned from right to left (`5` to `d`, `d` to `c`, and so on), but directly `5` to each one! See https://jsfiddle.net/rsja8ob3/ as a proof (Thanks @jalbee) – Ωmega Jan 27 '16 at 15:57
  • Interestingly, that is not necessarily true -- if you checkout the [Ecmascript Language Spec](http://www.ecma-international.org/ecma-262/5.1/#sec-11.13.1), the assignment operator actually returns the right-side value, not the left-side variable. This would allow for right-to-left assignment, even in that special case scenario. – jalbee May 16 '16 at 23:12