0

Saw something akin to the following in a js snippet:

var aaa = "eeee" + "rrrrr", bbb = "qqqqq" + "tttttt";

I'm confused, what is the advantage of doing that vs just:

var aaa = "eeee" + "rrrrr";
var bbb = "qqqqq" + "tttttt";
5
  • 1
    There's no advantage. It's just a personal preference. Commented Oct 14, 2014 at 15:22
  • micro-optimization.......... Commented Oct 14, 2014 at 15:23
  • 1
    Regarding your edit: I hope you know what var is doing and that there is a difference between using var and not using it (i.e. var aaa = "eeee" + "rrrrr" and aaa = "eeee" + "rrrrr";). Commented Oct 14, 2014 at 15:27
  • 1
    The title of your post is wrong. It's not a "variable assignment", it's a "variable declaration with initialization". Commented Oct 14, 2014 at 15:28
  • And it's not "weird" by any stretch of the imagination, it's fundamental JS syntax, and if anything probably more common than separate var statements. See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. Commented Oct 14, 2014 at 15:31

1 Answer 1

3

The comma, in this situation, allows you to declare multiple variables without having to re-use the var keyword over and over again:

var variable1 = 'foo',
    variable2 = 'bar';
Sign up to request clarification or add additional context in comments.

8 Comments

Except that the code snippet that OP mentioned isn't a declaration. It's a definition.
So this doesn't have anything to do with tuple notation then?
forgive me I cut and pasted and missed the var I'll fix that
@red888: You probably have to update your second code snippet as well. Or are you really asking what the difference between using var and not using it is? edit: apparently not :)
@red888 BTW some JavaScript interpreters do have destructuring assignment allowing you to do things like var { foo, bar } = { foo: 0, bar: 1 } and var [foo, bar] = [0, 1].
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.