I am trying to learn javascript and I just started trying to learn Arrays
I see problem when I use the array name as "name"
var names = ['asdasd','qweqwe'];
names[0];
returns "asdasd"
however,
var name = ['asdasd','qweqwe'];
name[0];
is returning "a"
why is that ? Any help is greatly appreciated.
2 Answers 2
It's probably in the global scope, so you're really setting window.name, and window.name already exists and can only be a string.
That's what you're returning, the string from window.name, not your name variable.
2 Comments
window.name is read-only. Rather, assignments to window.name gets converted to a string. So var name = ['asdasd','qweqwe']; results in window.name being asdasd,qweqwe, and name[0] produces the first letter a.Thats because name, being a predefined window property is a string and names is an array of string
You may confirm this by printing them individually.
name returns "asdasd,qweqwe" and names returns ["asdasd", "qweqwe"]