0

I have never seen this used, but my friend just told me that instead of writing:

var x = 1;
var y = 2;
var z = 3;

you can, and should, write:

var x = 1,
    y = 2,
    z = 3;

Never seen or heard of this, and not sure how it works, but I tried it on jsfiddle and it seems to work fine..?

Waffles
  • 113
  • 2
  • 10
  • 2
    What is the question? – Soren May 01 '16 at 23:46
  • You can also do `var x, y, z; x=1; y=2; z=3;`, it's all the same. – Niet the Dark Absol May 01 '16 at 23:47
  • They are both valid and equivalent at the end of the day. The `var` declaration takes a list of identifiers, with optional assignments, as part of the same statement; in the first example each `var` has a list of one such identifier/assignment. Your friend provided an *opinion* (made popular by some linting tools), which is fine - but it does not make the first form "wrong" or otherwise invalid. – user2864740 May 01 '16 at 23:48
  • 3
    The answer is yes, this is valid javascript. See this Q/A - http://stackoverflow.com/questions/694102/declaring-multiple-variables-in-javascript – bwegs May 01 '16 at 23:48
  • 1
    Questions on stack overflow should show evidence of basic research on the topic before posting and then should clearly state an actual question. This is an early step in any basic Javascript tutorial and, as you have already discovered, works as intended. This is valid Javascript. Don't see an actual question or problem statement here. – jfriend00 May 01 '16 at 23:54
  • http://www.ecma-international.org/ecma-262/6.0/index.html#sec-variable-statement – Felix Kling May 02 '16 at 00:24
  • Who on Earth up-voted this? – Yatrix May 02 '16 at 00:31

0 Answers0