user3651792 user3651792 - 8 months ago 23
Javascript Question

Find an Array Value using Two Variables

Suppose I set up a simple array:

var list = [

And then I set two variables, one to specify two variables: one for the name of the array, and another for the number, as such:

var array = "list";
var number = 0;

And then I run the following command:


But it outputs the number "1" instead of "Hello".

Can somebody explain this?


But it outputs the number "1" instead of "Hello". Any ideas?

No, it doesn't output 1, it's l(the alphabet L lowercased).

array is no longer an array when you assign a string list to it in

var array = "list";

and then


is equivalent of


which will return the character at zeroth index, thus returning l.


To reference an array, use the variable name(no quotes required)

var array = list; // Remove quotes.

If you don't want to reference same array and want to create a copy, use slice()

var copy = list.slice(0);