admin管理员组

文章数量:1129622

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...

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...

Share Improve this question edited Aug 6, 2019 at 5:12 Hasen 12.3k29 gold badges82 silver badges139 bronze badges asked Nov 12, 2010 at 16:27 HeatherKHeatherK 2,3234 gold badges20 silver badges12 bronze badges 1
  • 1 More on multiple assignment: How does variable assignment work in JavaScript?. I find this pattern useful inside a closure to expose constructor functions: var $cls = my.namespace.Foo = function(args){ ... } – blong Commented Feb 12, 2013 at 16:19
Add a comment  | 

10 Answers 10

Reset to default 170

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

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 = [];

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;

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.

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

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);

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

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!

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;

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);

本文标签: Javascript How to define multiple variables on a single lineStack Overflow