1

Possible Duplicate:
Difference between using var and not using var in JavaScript
What is the advantage of initializing multiple javascript variables with the same var keyword?

I have the following code:

var row = $link.attr('data-row'),
    a = 2,
    b = a;

Is this exactly the same as:

var row = $link.attr('data-row');
var a = 2;
var b = a;

When I use jslint it keeps suggesting I use just the one var. What do people normally do to make the code most readable. Also is there a way to stop jslint complaining?

Community
  • 1
  • 1
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427

1 Answers1

8

There is no practical difference. Use whichever suits you best. You can tell JSLint to allow many var statements by adding the following directive at the top the file:

/*jslint vars: true */

Note that there is no difference in speed between the two variants:

enter image description here

The part of the spec that deals with variable declarations states the following:

For each VariableDeclaration and VariableDeclarationNoIn d in code, in source text order do

  • Let dn be the Identifier in d.
  • Let varAlreadyDeclared be the result of calling env’s HasBinding concrete method passing dn as the argument.
  • If varAlreadyDeclared is false, then
    • Call env’s CreateMutableBinding concrete method passing dn and configurableBindings as the arguments.
    • Call env’s SetMutableBinding concrete method passing dn, undefined, and strict as the arguments.

There is nothing in there that would differentiate between separate and combined var statements.

James Allardice
  • 164,175
  • 21
  • 332
  • 312