3
See these variable statements:
var level = 0;
var atual_sequence = 0;
I could put them together like this?:
var level, atual_sequence = 0;
One would affect the functionality of the other?
3
See these variable statements:
var level = 0;
var atual_sequence = 0;
I could put them together like this?:
var level, atual_sequence = 0;
One would affect the functionality of the other?
3
The first question you should ask is: what advantage does it have in taking advantage of a variable name to do two different things?
In general the answer is none or it is just to type a few characters less, that would be a very questionable advantage. So it’s always better to give more readability, to make clear what the intention of each variable is. Not only do you use separate names for each thing, you give meaningful names of what you’re keeping in it.
Whether the doubt is to declare variables (in plurar) on the same line or not, depends a lot on the case, it is usually a matter of style but in general it is best to have each statement on each line:
var var1 = 0;
var var2 = 1;
it’s not so much better that
var var1 = 0, var2 = 1;
but in longer names, in larger lists, may become less readable.
You can do this too:
var var1 = 0,
var2 = 1;
But there is not much gain. And there may be disadvantages if you need to change the list of variables after you create an exception in the linearity of the syntax.
Care should be taken especially when both have the same value. There is a temptation to do this:
var var1 = var2 = 0;
This probably does not do what is expected. The variable var1
will have local scope and will be worth 0. Already the variable var2
will also be worth 0 but its scope will be global and not local, as many may assume.
You should also be careful if you do
var var1, var2 = 0;
Only var2
will have the value 0, var1
will not have defined value.
Then it would affect the functionality and even if it is what you want, which is unlikely, it is not recommended because it does not give clear indication of the intention.
2
Not, the comma (,
) do not join the variables, this is just a way to define them without writing so much, use var x, y;
and var x; var y;
are the same thing.
For example:
var x = 1, y =2;
console.log(x, y);//Vai mostrar duas variáveis do tipo `integer` assim: "1, 2"
The situation with line break:
var x = 1,
y =2;
console.log(x, y);//Vai mostrar duas variáveis do tipo `int` assim: "1, 2"
The isolated situation, ie each variable with its own var
:
var x = 1;
var y =2;
console.log(x, y);//Vai imprimir isto "1, 2"
In all cases we will have the same result, if you want to join two variables in a string, you will have to do something like:
var x = 1, y =2;
var z = '' + x + '' + y;
console.log(z);//Vai mostrar uma variável do tipo `string` assim: "12"
You can also use a array
to have all data in a "location" (it will depend on your need) and after this use the join
:
var x = [1, 2];
console.log(x.join(""));//Vai imprimir isto "12"
Browser other questions tagged javascript variables encoding-style variable-declaration
You are not signed in. Login or sign up in order to post.
Could explain better?
– Maniero
In the case are their different variables, indepentende to use
var x, y;
orvar x; var y;
, so one does not influence the other.– Guilherme Nascimento
How so?
1
and-24
are invalid names for variables in JS.– bfavaretto
The code no longer made much sense, now it is not even valid. Try to say exactly where you want to go and give real examples.
– Maniero
@Elaine show an example functional of the problem to understand what you did.
– Guilherme Nascimento
The question is very difficult to understand, but there seems to be a misunderstanding about multiple variable statements at once. When you use:
var x, y = 10;
, onlyy
has value 10, the value ofx
isundefined
. To complicate, in all your examples (including the ones you removed in the edit) the values seem to be on the wrong side of the assignment. The correct format isvar variavel = valor
, and not varvalor = variavel
. Can you please clarify?– bfavaretto
was a completely fictitious example, @Guilhermenascimento so they are autonomous then but with equal values, joining them would be the same as defining them separately?
– Elaine
Yes to
,
in variables do not join the two, it is just a simpler way to create the variables at runtime, as I saidvar x, y;
andvar x; var y;
are the same thing.– Guilherme Nascimento
Elaine, is there any doubt? The answers provided did not help you, please let us know if something is missing, if not, please mark the answer that you like the most as correct (if the answer resolved your doubt). Grateful.
– Guilherme Nascimento