Use multiple names with distinct functions in a variable

Asked

Viewed 117 times

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?

  • Could explain better?

  • 1

    In the case are their different variables, indepentende to use var x, y; or var x; var y;, so one does not influence the other.

  • How so? 1 and -24 are invalid names for variables in JS.

  • 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.

  • @Elaine show an example functional of the problem to understand what you did.

  • 1

    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;, only y has value 10, the value of x is undefined. 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 is var variavel = valor, and not var valor = variavel. Can you please clarify?

  • 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?

  • 1

    Yes to , in variables do not join the two, it is just a simpler way to create the variables at runtime, as I said var x, y; and var x; var y; are the same thing.

  • 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.

Show 4 more comments

2 answers

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.

Documentation.

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

You are not signed in. Login or sign up in order to post.