In JavaScript, it is possible to declare multiple variables like this:
var variable1 = "Hello World!";
var variable2 = "Testing...";
var variable3 = 42;
...or like this:
var variable1 = "Hello World!",
variable2 = "Testing...",
variable3 = 42;
Is one method better/faster than the other?
It's just a matter of personal preference. There is no difference between these two ways, other than a few bytes saved with the second form if you strip out the white space.
Besides maintainability, the first way eliminates possibility of accident global variables creation:
While the second way is less forgiving:
is more readable than:
But they do the same thing.
It's common to use one
var
statement per scope for organization. The way all "scopes" follow a similar pattern making the code more readable. Additionally, the engine "hoists" them all to the top anyway. So keeping your declarations together mimics what will actually happen more closely.