0

Is it possible to create a new variable with name equal to function argument?

var add = function(name, age) {
  var name = age;
}
add('bob', 30);

Obviously this doesn't work, but I hope you can see what I am trying to achieve.

w3me
  • 63
  • 1
  • 5
  • 1
    Any time you think you need to create variables dynamically, you're probably wrong. You should be using an object to map strings to values. – Barmar Mar 25 '17 at 00:45
  • No, that won't work, but as @Barmar says, you could do something like: `var object = {}; object[name] = age;` and then access it like `object['bob'];` – rorymorris89 Mar 25 '17 at 01:10

0 Answers0