0

The following code produces the same result in both options (Chrome & Firefox).

// short option
var a = b = c = d = e = 1;
console.log(a,b,c,d,e)

// long option
var a = 1, b = 1, c = 1, d = 1, e = 1;
console.log(a,b,c,d,e)

Perhaps the question for some seems obvious or silly but which of these two options is the correct one or should I use and why?

fwBasic
  • 154
  • 9

1 Answers1

3

The "short option" is almost certainly the wrong one to use, because it will either implicitly create global variables, or throw an error in strict mode:

'use strict';
var a = b = c = d = e = 1;
console.log(a, b, c, d, e)

Better to use the longer version.

But usually, in a situation like this, where you have multiple variables with somewhat similar purposes, it'd be more appropriate to use an array or object, rather than to have lots of separate identifiers. (Not always, but often)

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320