tags:

views:

96

answers:

5

Hello,

is there a good solution how can i define more than one var with the same value in one step an the beginning of my funcion?

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

Is there a good way to write this like var a,b = 0; ?

+2  A: 
var a = 0, b = 0;
Greg B
A: 

An alternate way

var a = b = 0; 
Kunal
Same problem as the answer by sAc
Fabien Ménager
+4  A: 

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

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

Better

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

Please take a look at http://javascript.crockford.com/code.html

Anders
I like Crockford, I really do, but whether it is better or not is completely subjective, especially the nonsense suggestion of alphabetically ordering the declarations.
Justin Johnson
What Justin said.
Tim Down
I agree, it's subjective. I presume the reader won't take everything I say, Crockford or anyone for that matter as gospel, but naturally form his/her own opinion.
Anders
Are you kidding? This is the world of jQuery. Of course novices will take what Crockford says as gospel ... as soon as they figure out who he is.
Justin Johnson
+2  A: 
var a = 0, b = a;
Sean
+4  A: 

You can't do two things at once. You can't declare multiple local variables and assign a single value to all of them at the same time. You can do either of the following

var a = 1, 
    b = 1;

or

var a,b;
a = b = 1;

What you don't want to do is

var a = b = 1;

because you'll end up with b being a global, and that's no good.

Justin Johnson