Skip to content

Declare one variable per var statement #1

Open

Description

@berryp

The guide is in favor of declaring multiple variables with only 1 var statement. For example:

var items = getItems(),
    goSportsTeam = true,
    dragonball = 'z';

This article presents some good argument against doing it that way . I agree with almost everything in this article; except I don't believe single var statement is more readable.

Better, would be:

var items = getItems();
var goSportsTeam = true;
var dragonball = 'z';

Activity

  1. jaraco commented on Mar 31, 2014

    @jaraco
    Collaborator

    When I read the examples on my phone, the four spaces indent didn't cause alignment with "var " due to proportional fonts. Perhaps tabs would have been better ;)

    var\tfoo;
    \tbar;

    Jk

    Sent from my comm

    On Mar 31, 2014, at 21:36, "Berry Phillips" <notifications@github.commailto:notifications@github.com> wrote:

    The guide is in favor of declaring multiple variables with only 1 var statement. For example:

    var items = getItems(),
    goSportsTeam = true,
    dragonball = 'z';

    Here's a good argument for this issue http://benalman.com/news/2012/05/multiple-var-statements-javascript/. I agree with almost everything in this article; except I don't believe single var statement is more readable.

    Reply to this email directly or view it on GitHubhttps://github.com//issues/1.

  2. berryp commented on Mar 31, 2014

    @berryp
    Author

    Well, if you used a "proper" phone...

    You did raise a good point there; I'm not a fan of lining up wrapped line alignment, partly for that reason. My general rule for wrapped lines is 2 x indentation.

  3. markrian commented on Apr 1, 2014

    @markrian

    Yeah, I'd say multiple var declarations are actually preferable - changes to them diff better, for one. That said, I wouldn't want to prohibit single var declarations. They can be useful if you want to declare, but not assign, a series of variables on a single line, e.g., var x, y, z;.

    Also, I really don't think it's necessary to put all of a scope's variable declarations at the top. Sometimes a variable only makes sense for a particular block further down, in which case putting at the top is just noise. JS developers ought to know about var hoisting, and the consequences of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions