How to define several variables in one line?

Reading the documentation online, I got confused how to correctly define multiple JavaScript variables in one line.

If I want to condense the following code, what is the correct "strict" JavaScript way to define multiple javascript variables on the same line?

var a = 0; var b = 0; 

It:

 var a = b = 0; 

or

 var a = var b = 0; 

etc...

+82
javascript
Nov 12 '10 at 16:27
source share
7 answers

You want to rely on a comma, because if you rely on a design with several assignments, you will shoot in the leg at some point.

Example:

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

They all refer to the same object in memory, they are not "unique", because at any time you make a reference to the object (array, object literal, function), which it passed by reference, and not by value. Therefore, if you change only one of these variables and want them to act individually, you will not get what you want, because these are not separate objects.

There is also a drawback to multiple assignments, as secondary variables become global and you don’t want to seep into the global namespace.

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

It is best to use commas and preferably with a lot of spaces so that it reads:

 var a = 5 , b = 2 , c = 3 , d = {} , e = []; 
+66
Nov 12 '10 at 16:27
source share

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

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

And if you want to easily print multiple variables on one 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.

+42
Aug 25 '16 at 22:30
source share

There is no way to do this on the same line with assignment as a value.

 var a = b = 0; 

makes b global. The correct path (without variable leaks) is a little longer:

 var a = 0, b = a; 

which is useful in case of:

 var a = <someLargeExpressionHere>, b = a, c = a, d = a; 
+38
Nov 12 '10 at 16:30
source share

Why not do 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 of variables, since this:

 var a = b = c = d = 1; 

will result in implicit declarations b , c and d in the window area.

+23
Jul 15 '16 at 16:56
source share

Here is the new ES6 method of declaring multiple variables on the same 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 must be the same

+4
Feb 19 '19 at 17:16
source share

In particular, to what OP requested, if you want to initialize N variables with the same value (for example, 0 ), you can use the destructuring array and Array.fill to assign an array of N 0 s to the variables:

 let [a, b, c, d] = Array(4).fill(0); console.log(a, b, c, d); 
+2
Mar 05 '19 at 22:02
source share

Note that 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; 
0
Jul 08 '14 at 16:23
source share



All Articles