Define multiple variables in one step?

Is there a solution I can use that allows me to define more than one var with the same value in one step at the beginning of my function?

function myFunction () {
    var a,b = 0;
    document.write(a) // undefined
    document.write(b) // 0
}

Is there an improved recording method a,b = 0;?

+3
source share
5 answers

Something like this, however I don't like it.

var var1 = "hello",
    var2 = "world",
    var3 = 666;

it's better

var var1 = "hello";
var var2 = "world";
var var3 = 666;

Please see http://javascript.crockford.com/code.html

+6
source

You cannot do two things at once. You cannot declare several local variables and simultaneously assign one value to all of them. You can do one of the following:

var a = 1, 
    b = 1;

or

var a,b;
a = b = 1;

What you do not want to do is

var a = b = 1;

b, , .

+4
var a = 0, b = 0;
+2
source
var a = 0, b = a;
+2
source

Alternative way

var a = b = 0; 
-1
source

Source: https://habr.com/ru/post/1754951/


All Articles