1

I want to refer to a variable by another variable. 1st variable's value is a string equal to 2nd variable's name. How to do, say:

var1 = "var2";
var2 = [1,2,3];

console.log(var1[0]);

and get 1 instead of 'v'?

my code:

localStorage.setItem('scn', 'AR1');

var AR1 = [1,3,4,6];
var AR2 = [1,3,5,6];
var AS2 = [1,3,7,8,9,10];

here_idx = localStorage.getItem('scn').indexOf(1);

In this case here_idx is equal to 2 instead of 0.

I tried parsing it with an error. I don't know how to even search for such problem online. I tried.

1
  • Try in the same scope where you initialized var1: [var1][0] Commented Jul 14, 2020 at 21:32

4 Answers 4

3

You can use window[varname] to get a previously declared variable with var (not let or const) that is in the window scope.

Fiddle Since Snippets don't allow localstorage: https://jsfiddle.net/o27andcx/

var AR1 = [1,3,4,6];
var AR2 = [1,3,5,6];
var AS2 = [1,3,7,8,9,10];

localStorage.setItem('scn', 'AR1');

here_var = localStorage.getItem('scn');

here_idx = window[here_var];

console.log(here_idx);
Sign up to request clarification or add additional context in comments.

3 Comments

Note that this will only work for variables declared with var, not those declared with let or const. If you want to make a variable global, it's usually clearer to declare it explicitly as a property of window: window.AR1 = ... etc.
Doesn't work for me. Results in the same case as in my code.
@Warxuaroz that code does work here is a fiddle: jsfiddle.net/o27andcx So you must not being using it right or your code example doesn't show what you are actually doing
0

You can assign variables from arrays with destructured assignment

const [z, x, ,y] = [1, 2, 3, 4, 5, 6];
console.log(z, x, y);     //Outputs [1, 2, 4]

1 Comment

Would you expand this answer? I have no idea what happend here.
0

"var2" and var2 are not the same. You can do something like the following

var1 = var2;
var2 = [1,2,3];

console.log(var1[0]); // outputs 1 not 'v'

or

var AR1 = [1,3,4,6];
localStorage.setItem('scn', AR1);

here_idx = localStorage.getItem('scn').indexOf(1);//outputs 0

3 Comments

That would work, but I need to keep var1 and var2 unchanged for later use.
Unless you change them variables will remain unchanged. So unsure what the issue is. What is the use case for assigning a variable name as string?
I'm following a scenario named AR1, which I want to know in every .html during the scenario, hence I store it in localStorage under 'scn'. I need to also know, where to go with my 'back' and 'next' buttons, hence the AR1 array with all the .htmls' names.
-1

One way to do it. But not recommended is as follows: (using eval() ).

It is fully legal and valid though:

var2 = [1,2,3];
var1 = eval("var2");
console.log(var1[0]);

3 Comments

Using eval is not recommeded. See this
I did say that already in my answer. But it is not illegal or invalid as it is fully supported. It is not a deprecated keyword.
It's the only suggested answer, that works in my case. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.