Poll: JavaScript Var Declaration

By  on  

Hardcore developers of any language are generally very opinionated when it comes to the specifics of how they code. In the JavaScript community, one subject of heated debate is whether to use the `var` keyword once and separate declarations by line:

var one = 1,
	two = 2,
	three = 3;

or use the `var` keyword many times, one declaration per line:

var one = 1;
var two = 2;
var three = 3;

The question is...what do you prefer?

Recent Features

  • By
    Being a Dev Dad

    I get asked loads of questions every day but I'm always surprised that they're rarely questions about code or even tech -- many of the questions I get are more about non-dev stuff like what my office is like, what software I use, and oftentimes...

  • By
    Responsive and Infinitely Scalable JS Animations

    Back in late 2012 it was not easy to find open source projects using requestAnimationFrame() - this is the hook that allows Javascript code to synchronize with a web browser's native paint loop. Animations using this method can run at 60 fps and deliver fantastic...

Incredible Demos

  • By
    Advanced CSS Printing – Using JavaScript Double-Click To Remove Unwanted DIVs

    Like any good programmer, I'm constantly searching around the internet for ideas and articles that can help me improve my code. There are thousands of talented programmers out there so I stumble upon some great articles and code snippets that I like to print out...

  • By
    Fix Anchor URLs Using MooTools 1.2

    The administrative control panel I build for my customers features FCKEditor, a powerful WYSIWYG editor that allows the customer to add links, bold text, create ordered lists, and so on. I provide training and documentation to the customers but many times they simply forget to...

Discussion

  1. I voted for the single var declaration, preferably with all the variable names and equal signs vertically aligned, looks much readable for me.

  2. There’s a precise reason why I prefer multiple variable declaration, and it is matching. It is much easier to do matching over multiple `var` declarations than to consider a case where you got them mixed up :)
    You can still solve that, but it really doesn’t make much sense to be inconsistent.

  3. andy

    ideally you use both. use one var for making a list of simple variables. if you need to declare a function with var, I usually prefer to do it on a new line with its own var. declaring a function in a var list means it has to be extra indented, which sucks, so i’m ok using an extra var to declare a big private function

  4. Rolf

    Switched from multiple to single a few years ago

  5. I voted the single declaration, because i used that from the beginning, and IMHO it is more easier than the other.
    Sorry for my bad english :D

  6. Both are same working but, single var statement is preferable from my point of view.

  7. piotr

    I’m using one var declaration for multiple variables, all but first intended by tab. Most IDEs are configured by default that tab is 4 chars wide, so all variables are seamlessly aligned.

    I’m playing with idea of leaving out semicolon at the end so it’s easier to add new variables.

    I try to put var statement at the top of function, so while coding inside I can always peek for hints in the top. Sometimes I predefine type.

    /**
     * Method description
     *
     * @param  {String}  text
     * @return  {String}  Manipulated input string
     */
    onSuccess: function( text )
    {
    	var wtf = true,
    		// Comment for timestamp if variable name is confusing
    		timestamp = new Date(),
    		output = ''
    
    
    	output = text + ' on ' + timestamp.toString();
    
    	if ( wtf ) 
    		output += ' , but wtf?';
    
    
    	return output;
    }
    
  8. piotr

    oh great, all intends are lost :[
    Method body should be intedend by 1 tab, variables after var and output += by 2 tabs

  9. Alex

    I use multiple var declaration because it makes my code more readable :)

  10. I voted for multiple var declarations. Its a personal preferance – I have no reason. However I wouldn’t mix or match as that would make the code less readable.

  11. As i work in different projects, with different tab settings, always assuming a tab width of 4 chars isnt an option. In those cases, using a single var makes the code look sloppy, as it does to me in the example on this website.
    Using multiple vars makes it more consistent across different editor settings and far more readable in my opinion.

  12. I voted for multiple var declarations, but i prefer write coffeescript ;)

  13. Here you will find a great article regarding this long discussion between javascript developers https://twitter.com/alexborbely/status/209565943150546945

  14. kiwi

    I’ve voted for multiple declaration but it really depends on the situation, for example:

    var a = 1;
    var b = 2;

    but what if I want to set a and b with the same value?

    var a = b = 3; //maybe not very clear but in resetting values this is a much faster option

    var start = end = temporary = finished = null;

    Also when I have just to declare new variables but without setting any value I siply do the following:

    var a, b, c , d;

    What I can’t stand is the colomn on the new line eg:

    var a = 1
    , b = 2
    , c= 3;

    For this reason I think it depends on the situation but multiple declaration is the way to go :)

  15. kiwi

    Holy crap, what happened to the intends….

  16. Stephen Mathieson

    Coding style aside, one var declaration is actually the way JavaScript works.

    https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/var

  17. Harpreet Singh Sahota

    Please share poll results too. :)

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!