1
(function(){
    var b1 = b2 = b3 = b4 = 100;
})();

console.log(b1); //100
console.log(b2); //100
console.log(b3); //100
console.log(b4); //100

Here assignment is right associative and hence 100 is assigned.

It is a bad practice. Better version is

(function(){
  'use strict';
   var b1 = 100,
   b2 = 100,
   b3 = 100,
   b4 = 100;
})();

Question: Is there any case where such wrong assignment

var b1 = b2 = b3 = b4 = 100; 

could be preferred or should it never be used?

user544079
  • 16,109
  • 42
  • 115
  • 171
  • 1
    I think your time would be better spent analyzing problems which require solutions. – Travis J Jan 09 '15 at 00:23
  • Bad practice, my ass. If you have four values that are intended to be equal, chained assignment is the single most reliable way to do it. Though you do want the variables to exist first. – cHao Jan 09 '15 at 00:23
  • 2
    @Mathletics it's **definitely** not "opinion-based". The `var` statement only declares the variable to the left of the first (left-most) `=` sign. The rest are implicit global references. – Pointy Jan 09 '15 at 00:25
  • @cHao I hope you don't really do this, because it's wrong. – Pointy Jan 09 '15 at 00:27
  • @Pointy: The way it's being done up there, yeah, it's wrong. But there's absolutely nothing wrong with `a = b = c = d = 0;`. – cHao Jan 09 '15 at 00:31
  • @cHao oh yes; it's not bad because it's a "cheat" or because it's ugly; in this case it's bad because it's wrong. I don't see any problem with it for already-declared variables, and in fact I think it's actually more readable than a list of separate assignments. – Pointy Jan 09 '15 at 00:32
  • @Pointy whoops, I missed that! – Evan Davis Jan 09 '15 at 00:35

1 Answers1

5

It is not okay, since those 2 code examples are not identical. The first one equals to:

var b1;
b4 = 100;
b3 = b4;
b2 = b3;
b1 = b2;

So you only define b1 in the local scope, the b2..b4 are declared globally. Which means this MUST BE completely avoided.

I also highly doubt console.log(b1); outputs 100 as per your example.

(function(){
    var b1 = b2 = b3 = b4 = 100;
})();

console.log(b1); //100 <-- this is not true
console.log(b2); //100
console.log(b3); //100
console.log(b4); //100

From the other hand - for already declared variables - initialization or assignment using

// we assume a, b and c have been declared already
a = b = c = 100;

is a subject of your project/team conventions. From technical perspective it's fine to use it.

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • 2
    You're mostly right but there's no reason to expect that `b1` won't end up with the right value, is there? – Pointy Jan 09 '15 at 00:26
  • On the initial portion of the answer, I say, "Precisely!" On the second part, I don't think that's correct. – JAAulde Jan 09 '15 at 00:26
  • @Pointy: I'm not sure I see your point. – zerkms Jan 09 '15 at 00:26
  • I downvoted for the last part. Did you try it? console.log(b1) outputs 100 in both your example and the OP's. Will retract if corrected or at least explained. – Travis J Jan 09 '15 at 00:27
  • @zerkms well assuming the implicit global definitions all go OK (which with names like that they probably will), why wouldn't `b1` end up being `100`? (I'm not trying to be snarky; I've got a cold and I don't 100% trust my judgement.) – Pointy Jan 09 '15 at 00:28
  • @TravisJ http://jsfiddle.net/r7xc2vcL/ please check the OP's example carefully - they define it in an IIFE – zerkms Jan 09 '15 at 00:28
  • @Pointy see the comment right above – zerkms Jan 09 '15 at 00:28
  • 1
    @zerkms - I see, you meant with the IIFE context there. Yes, I will retract you are correct. – Travis J Jan 09 '15 at 00:29
  • 1
    @zerkms oh durr. Like I said, I don't trust my judgement :) – Pointy Jan 09 '15 at 00:30
  • 1
    Actually, @zerkms is completely right. I failed to take into account the location of the calls to `console.log` in the OP. – JAAulde Jan 09 '15 at 00:30