0

I want to be able to create a variable with a variable as a name, not value. That probably sounds confusing, so here's some code that would be an example for what I want to do-

function customVar(variable) {
  var variable = 1
}

Obviously, that makes a variable called "variable", which is not what I want, but you get the idea.

  • 2
    You already have a variable called variable. It's on your argument list – Taplar Jul 03 '20 at 18:09
  • 4
    Easiest way to do this is to just `var myobj = {}; myobj[variable] = whatever` – Taplar Jul 03 '20 at 18:10
  • 4
    What would be the point of that. – Pointy Jul 03 '20 at 18:10
  • @Taplar that's not what I mean. – Gabriel Wood Jul 03 '20 at 18:11
  • Your variable is scoped to the function. Its name has no meaning other than as a symbol for reference **inside the function**. – Pointy Jul 03 '20 at 18:12
  • 4
    Pointy's first comment is also an important question. Out side of just for knowledge sake, this question screams "why?". What problem would this solve for you? – Taplar Jul 03 '20 at 18:13
  • @Taplar Thanks, that worked! – Gabriel Wood Jul 03 '20 at 18:14
  • @Taplar I made a function that creates a textbox. I will use it many times, and each time it will be in response to a different question, so I want it to store the answer in a different variable. I could have used an array, but then I'd have to keep track of which answer is where in the array, so I'm going to do this. Thanks for your answer, by the way! – Gabriel Wood Jul 03 '20 at 18:16
  • You can also use data properties to associate extra information with dom elements, that are non-standard. So for argument sake, you could store the answer to a question as a data property at which point the variable reference is always the same. – Taplar Jul 03 '20 at 18:18
  • @Pointy see my response to Taplar. – Gabriel Wood Jul 03 '20 at 18:18
  • @Taplar I could, but this seems simpler. – Gabriel Wood Jul 03 '20 at 18:19
  • Heh, our concepts of 'simple' seem to differ, :) Like, you could also have an array of `{ question: , answer: }` for all the questions, at which point the references also become uniform and not a variable. Lots of ways to go about it, without jumping to variable variables. – Taplar Jul 03 '20 at 18:19

0 Answers0