Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set multiple local variables to the same value in a single declaration?

Tags:

javascript

Consider the following:

(function(){
   var foo = bar = 1;
}());
  • foo will be a local variable to the function
  • bar will be a global variable to the window

Due to their scoping, both variables will have a value of 1 inside the function, but bar will persist outside the function (in global scope).

I'm curious if there is a way to initialize variables using the assignment operator, without a loop or an object. I'm looking for a keyword or prefix that would make bar locally scoped. The idea is to be DRY and efficient.


Edit: The example above is simple. One option, using 10 variables, might be to predeclare the variable to the local scope before initializing it:

var foo, bar, baz, foobar, foobaz, bazfoo, barbaz,
    bazbar = foo = bar = baz = foobar = foobaz = barbaz = true;

However, doing so is both repetitive and harder to read. A person could use an array or object, but that is a little less clear and more cluttered.

The background was that another developer was doing var foo = bar = true inside a function, which I pointed out is only setting one local variable. They were not aware of that and wanted to change and asked if it's possible to inline and set multiple local variables, without typing each one multiple times (and not refactor code w/ array or object).

Upon originally asking this question, I was exposed to ES6, but had yet to fully read the docs. My thinking was that ES6 has some nice features (e.g., rest, destructuring assignment) and so perhaps it also provided a way to massively initialize a large block of variables to the same scope and value.

like image 508
vol7ron Avatar asked Mar 09 '16 22:03

vol7ron


People also ask

How to declare more than one local variable in a statement?

In C#, you can use the comma to declare more than one local variable in a statement. The following displays the same − Let us see an example in which we are declaring multiple local variables.

Can one declare multiple variables in one line using the local keyword?

Say one have a function/method foo. Can one use the local keyword to declare multiple variables in one line, or do they have to be separated by one declare statement for each variable? foo () { local -i x -a y z } foo () { local -i x=2 -a y= () z } … or the equivalent one by one line declaration. Yes.

How to declare multiple variables with the same value in JavaScript?

Then, when you want to declare multiple variable with the same value, you can Assign the same value to each variable: var var1 = value, var2 = value, /* ... */ varN = value; In case value is an expensive expression, you can use var1 instead of repeating value to avoid recalculating it each time, e.g. var foo = 1, bar = foo;

How to declare all variables first and assign to all values?

Declare all variables first, and then assign the values to all of them: You can do it in a single statement and avoid repeating the last variable, e.g. If you really want to avoid repeating the variables and the values, you could try a destructuring assignment with [].fill: But I don't recommend it because it will create an useless array.


1 Answers

You can't (or shouldn't) assign a value to an undeclared variable.

Then, when you want to declare multiple variable with the same value, you can

  • Assign the same value to each variable:

    var var1 = value,
        var2 = value,
        /* ... */
        varN = value;
    

    In case value is an expensive expression, you can use var1 instead of repeating value to avoid recalculating it each time, e.g.

    var foo = 1, bar = foo;
    
  • Declare all variables first, and then assign the values to all of them:

    var var1, var2, /* ..., */ varN;
    var1 = var2 = /* ... = */ varN = value;
    

    You can do it in a single statement and avoid repeating the last variable, e.g.

    var foo, bar = foo = 1;
    
  • If you really want to avoid repeating the variables and the values, you could try a destructuring assignment with [].fill:

    var [var1, /*...*/ varN] = new Array(n).fill(value);
    

    But I don't recommend it because it will create an useless array.

like image 92
Oriol Avatar answered Sep 21 '22 14:09

Oriol