1
let varone = "vartwo";
let vartwo = "answer";

And I would like to get "answer". But I cannot do the following:

console.log(vartwo);

How can I get "answer", but not by including "vartwo" in my code?

zzz
  • 71
  • 1
  • 1
  • 6
  • You cannot form variable references dynamically, but you *can* do it for object properties. In my experience, there's almost always a better way of doing things, though it's hard to be specific because your posted code doesn't explain a lot about your actual problem. – Pointy Mar 12 '21 at 21:38

1 Answers1

2

I think one way to resolve it, is by using javascript objects, as shown:

    obj={
    varone:"vartwo",
    vartwo:"answer"
    };
   console.log(obj[varone]) //outputs "answer"