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.
Not the name of the variable with the value of the other, in case create a variable with the name confirmName1
– user45722
got confused the way he described.. but you can understand.. despite this, just correct rather than put in the comment..
– Daniel Omine
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.
– Daniel Omine
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.– bfavaretto