0

I see a lot of javascript code that does this:

var a = something, 
    b = else,
    c = foo;

Like every coding style issue, this is all a matter of taste and consistency but I wonder if there is a specific technical rationale behind this as opposed to:

var a = something;
var b = else;
var c = foo;
mathieu
  • 2,954
  • 2
  • 20
  • 31
  • It's mostly a matter of style and, as http://stackoverflow.com/questions/3781406/javascript-variable-definition-commas-vs-semicolons points out, it might save some in terms of file size because you're using fewer characters. (More in the next comment.) – Derek Henderson Jun 20 '13 at 09:07
  • 2
    However, there is another often overlooked advantage to declaring `var` only once and separating the different vars with commas. In JS, all variables and functions are hoisted to the top of the function in scope, so there is a performance advantage of having all the vars at the top, sparing the JS engine from having to do it for you. If you only use one `var` declaration per function, it's easier to keep track of all your vars and prevent any of them from getting hoisted, so it's a good habit to develop. – Derek Henderson Jun 20 '13 at 09:08

0 Answers0