Make Variable with multiple names and a single Value String

Asked

Viewed 737 times

1

It would be possible to create a single variable that has mutual influence by other names in it.

In other words, different names for the same statement var, and in the end they will have the same value.

Example

I’m doing like this:

var perfume = "Racco";

var colonia = "Racco";

var roll-on = "Racco";

I wish something like that:

var perfume, colonia, roll-on = "Racco"

It doesn’t matter! Any of the three names, matches the same new String();.

Just one var, with several names, to call only a single Valor.

How to do this? After declared more than one variable on the same line bring a single value for all of them.

1 answer

5


You can assign the same value to several variables at once as follows:

var perfume = colonia = rollon = "Racco"

But in the case above, if the variables colonia and rollon not yet declared, they will be global variables.
If you want to create variables for the local scope and assign the same value to them you need to do both in separate steps:

var perfume, colonia, rollon;
perfume = colonia = rollon = "Racco";

If in fact what you want is to change one variable and have the value "automatically" changed in the others: No javascript this is not possible with primitive types, only with complex types, as they are passed as reference:

var objeto = {};
objeto.x = 10;
var objeto_2 = objeto;
objeto_2.x = 20;
console.log(objeto.x); // objeto.x == 20

Browser other questions tagged

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