Create variable from string

Asked

Viewed 1,392 times

0

Suppose I did have:

var develop = 'confirmName1'

And I want to create a new variable from the value of variable develop ("confirmName1"):

var 'confirmName1' = 'valorDaNovaVariavel'

alert(confrirmName); // Retornar: (String) valorDaNovaVariavel

In PHP I know, but apparently it doesn’t work the same way in Javascript.

  • Not the name of the variable with the value of the other, in case create a variable with the name confirmName1

  • got confused the way he described.. but you can understand.. despite this, just correct rather than put in the comment..

  • technically speaking, the term closest to the equivalent in PHP would be "variable". It is to use the value of a variable to name another variable.

  • Considering what you posted below as an answer (it is not an answer, and so it has already been deleted from there), I think you better post a new question and in it put more details of what you need to do and why you want to use this method. Probably has better alternatives than the suggestions of eval and global variables - which answer this question in the way it is formulated, but can and should be avoided in most cases.

3 answers

5


There is a technique that you can do this. It’s kind of like this:

var x = "nome"; 
eval("var " + x + " = 'Tonho';");
console.log("Valor da variável nome:", nome);

As you can see above, the function that makes the "magic" is called val, but there are a number of restrictions that function use, especially when it comes to security...

Don’t use Unnecessarily! Val() is a dangerous function, which executes the code passed with the Caller privileges. If you run Eval() with a string that can be affected by a malicious person, you may end up running code malicious on the user’s machine with the permissions of its page/extension. Most importantly, third-party code can see the scope in which Eval() was called, which may lead to possible Attacks as Function is not susceptible.

Know the full function here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval

### UPDATING ###

There is a more "clean" and safer way to do this. See:

var x = "nome";
window[x] = "Tonho";
document.getElementById("resultado").textContent = "Valor da variável nome: " + nome;
<h3 id="resultado"></h3>

It is important to note that the dynamically created variable will be in the global scope of the application.

I hope I’ve helped.

2

Follow a simple example:

var chave = 'valor';

window[chave] = 'valor';

document.write(valor); // valor = valor

1

There are different ways to write this friend.

If you work with the concept of screens, I suggest something similar to this:

var tela = document.body;
var nameVar = "somar";
tela[nameVar] =  3.14 * 3.14;

//mais codigo e bla bla bla..
console.log("resultado", tela.somar);

//tambem pode recuperar o valor da var assim:
console.log("valor da var", tela[nameVar]);

Browser other questions tagged

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