170

Reading documentation online, I'm getting confused how to properly define multiple JavaScript variables on a single line.

If I want to condense the following code, what's the proper JavaScript "strict" way to define multiple javascript variables on a single line?

var a = 0;
var b = 0;

Is it:

var a = b = 0;

or

var a = var b = 0; 

etc...

Hasen
  • 11,710
  • 23
  • 77
  • 135
HeatherK
  • 2,293
  • 4
  • 20
  • 12
  • 1
    More on multiple assignment: [How does variable assignment work in JavaScript?](http://stackoverflow.com/q/509579/320399). I find this pattern useful inside a closure to expose constructor functions: `var $cls = my.namespace.Foo = function(args){ ... }` – blong Feb 12 '13 at 16:19

10 Answers10

164

Using Javascript's es6 or node, you can do the following:

var [a,b,c,d] = [0,1,2,3]

And if you want to easily print multiple variables in a single line, just do this:

console.log(a, b, c, d)

0 1 2 3

This is similar to @alex gray 's answer here, but this example is in Javascript instead of CoffeeScript.

Note that this uses Javascript's array destructuring assignment

modulitos
  • 14,737
  • 16
  • 67
  • 110
100

You want to rely on commas because if you rely on the multiple assignment construct, you'll shoot yourself in the foot at one point or another.

An example would be:

>>> var a = b = c = [];
>>> c.push(1)
[1]
>>> a
[1]

They all refer to the same object in memory, they are not "unique" since anytime you make a reference to an object ( array, object literal, function ) it's passed by reference and not value. So if you change just one of those variables, and wanted them to act individually you will not get what you want because they are not individual objects.

There is also a downside in multiple assignment, in that the secondary variables become globals, and you don't want to leak into the global namespace.

(function() {  var a = global = 5 })();
alert(window.global) // 5

It's best to just use commas and preferably with lots of whitespace so it's readable:

var a = 5
  , b = 2
  , c = 3
  , d = {}
  , e = [];
meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
58

There is no way to do it in one line with assignment as value.

var a = b = 0;

makes b global. A correct way (without leaking variables) is the slightly longer:

var a = 0, b = a;

which is useful in the case:

var a = <someLargeExpressionHere>, b = a, c = a, d = a;
Andy Jenkins
  • 13
  • 1
  • 5
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
57

Why not doing it in two lines?

var a, b, c, d;    // All in the same scope
a = b = c = d = 1; // Set value to all.

The reason why, is to preserve the local scope on variable declarations, as this:

var a = b = c = d = 1;

will lead to the implicit declarations of b, c and d on the window scope.

Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54
  • 1
    This was useful for me because I declare many variables upfront in my program. Now, instead of taking 30+ lines I can use a few lines without sacrificing readability. – Kalif Vaughn Mar 09 '20 at 15:09
  • 1
    This succinctly clarifies (and improves upon) the accepted answer, it's as simple and reliable and doesn't require unnecessary ES6 destructuring or array methods. – lys Apr 13 '23 at 00:34
24

Here is the new ES6 method of declaration multiple variables in one line:

const person = { name: 'Prince', age: 22, id: 1 };

let {name, age, id} = person;

console.log(name);
console.log(age);
console.log(id);

* Your variable name and object index need be same

Prince Ahmed
  • 1,038
  • 10
  • 10
15

Specifically to what the OP has asked, if you want to initialize N variables with the same value (e.g. 0), you can use array destructuring and Array.fill to assign to the variables an array of N 0s:

let [a, b, c, d] = Array(4).fill(0);

console.log(a, b, c, d);
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
2

The compressed type of that is here:

var a, b = a = "Hi";

& for 3 variables:

var x, y, z = x = y = "Hello";

Hope to be helpful!

1

do this if they have same value

let x = y = z = 0

otherwise

let [x, y, z] = [10, 30, 50]
console.log(x, y, z) // 10 30 50
YassBan
  • 65
  • 10
0

note you can only do this with Numbers and Strings

you could do...

var a, b, c; a = b = c = 0; //but why?

c++;
// c = 1, b = 0, a = 0;
isherwood
  • 58,414
  • 16
  • 114
  • 157
PDA
  • 766
  • 6
  • 10
-2

This is completely correct:

var str1 = str2 = str3 = "value";

And if change one of their value, the value of other variables won't change:

var str1 = str2 = str3 = "value";

/* Changing value of str2 */
str2 = "Hi Web!";

document.write("str1 = " + str1 + " - str2 = " + str2 + " - str3 = " + str3);
CodeIt
  • 3,492
  • 3
  • 26
  • 37
CHP
  • 1