3

I have an array inside an array, and somehow it doesn't recognize the different values as separate, so that if I try to print the 2nd value it prints me the 2nd letter of the first value.

var name = ["John", "Alex"];
var food = ['pizza', 'banana', "cheese", ["bmw", "tesla"], name];
document.write(food[4][1]);

The array ['bmw', 'tesla'] works fine, its the name-array that acts weird.

food[4][0] outputs J (from John)

food[4][5] outputs A (from Alex).
2
  • I tried, and it works perfectly fine. Better try to check you declaration of array. you might declare name as a string instead of array (because that is the only possible reason food[4][5] equal to A) or else because you use reserved word in Javascript Commented Mar 12, 2018 at 11:58
  • changing from "name" to something else fixed it. But what did you mean by declaration of array? It's declarated as array if it has square brackets right? In php you can do array(), which I tried here and didnt work. How else could I declare an array? Thanks Commented Mar 12, 2018 at 14:40

3 Answers 3

6

It depends where you run the var name = ["John", "Alex"]; statement.

If you put it into a function then it works as you expect it.

But if you run it in the browser's console then it is executed in the global scope. The variables declared in the global scope, are properties of the global window object. But the window object already has a property named name and it's type cannot be changed. If you assign to it a value that is not string, its method toString() is used to convert it to a string.

This is why the statement:

var name = ["John", "Alex"];

assigns a new value to window.name and, because its type cannot be changed, the array ["John", "Alex"] is converted to the string "John,Alex" and assigned to window.name.

Use a different name for the variable (names, f.e.) and it will work as you expect it even in the global context.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that solved it. And thank you for the explanation, that helped even more!
3

You should also avoid using the name of JavaScript built-in objects, properties, and methods:

as i understand you should avoid name keyword to use because it is reserved. try to use names it should work.

1 Comment

Not really a reserved word - more something like a predefined identifier in the browser. axiac answers it.
-1

Thats because food[4] is not array but string "John,Alex"

food[4].split(',')[0] outputs "John"
food[4].split(',')[1] outputs Alex

4 Comments

And why is it not a reference to the other array?
Because food[4] is a string type not an array. Therefore if you do food[4][0] it would output letter at position zero of a string.By using split command you actually create an array which can be accesed to use elements.
The question is why is it a string when it's defined as an array. It's a very peculiar issue relating to how var s work in browser's global scope and the coincidence of using a variable named like an existing window property. axiac's answer nails it, you should read it
You are indeed correct. Thanks, I completely missed that...

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.