Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

var x, y = 'foo'; Can this be accomplished?

Since it is possible to do:

var x = 'foo', y = 'foo';

Would this also be possible?

var x, y = 'foo';

I tried it, however x becomes undefined.

I know this may seem like a silly or redundant question, but if I'm curious about something, why not ask? Also you will probably wonder why I would need two variables equal to the same thing in scope. That is not the point of the question. I'm just curious.

like image 220
daryl Avatar asked Aug 20 '11 04:08

daryl


People also ask

Can var be Redeclared?

var is function scoped. let does not allow to redeclare variables. var allows to redeclare variables.

Can var be a function?

The var statement declares a function-scoped or globally-scoped variable, optionally initializing it to a value.

How do you solve for variables XY?

Var[X+Y] = Var[X] + Var[Y] + 2∙Cov[X,Y] .

What is the purpose of using VAR?

var is a keyword, it is used to declare an implicit type variable, that specifies the type of a variable based on initial value.


1 Answers

Not sure if this is what you're asking, but if you mean "Can I assign two variables to the same literal in one line without typing the literal twice?" then the answer is yes:

var x = 10, y = x;
like image 138
Phil Avatar answered Oct 26 '22 07:10

Phil