2

I checked out a github project recently, and I found the following code in one of the files:

var fu = $('#fileupload').data('blueimpFileupload'),
      template;

It seems like this is assigning one variable to two values. What is it actually doing? Someone in my office mentioned that it may be the equivalent of just saying:

var f = $('#some-id').data('string');
var template;

Is this the case?

kingsfoil
  • 3,795
  • 7
  • 32
  • 56

2 Answers2

4

Your colleague is correct: you are declaring two variables, and assigning only to the first. The two statements are equivalent.

peter-b
  • 4,073
  • 6
  • 31
  • 43
  • Weird way to do it tho, didn't knew that was even possible. – Vadorequest Oct 30 '14 at 22:27
  • Is this preferred for some reason? It seems somewhat inane. – kingsfoil Oct 30 '14 at 22:27
  • @alex0112: There is no advantage or disadvantage from a programmatic point of view. However, the style guide you are following might dictate a certain way to declare variables. – Felix Kling Oct 30 '14 at 22:31
  • 1
    It saves you having to write `var` a lot I guess. Seems like a matter of personal preference really as to whether or not you use it. This answer (http://stackoverflow.com/questions/694102/declaring-multiple-variables-in-javascript) argues for the second way (each `var` separately). – peter-b Oct 30 '14 at 22:31
0

The declaration is not uncommon, quite common in fact, its a requirement of JSLint, to have a single var , and all the variables declared at the top as it would be done by hoisting anyways (but not necesserily in a single line).

On the other hand, there's a well accepted convention for declaring the variables in separate lines, sorted alphabetically. The declaration you see is passing JSLint check and adhering to convetion

Master Slave
  • 27,771
  • 4
  • 57
  • 55