0

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

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.

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

2 Comments

I don't think 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.
@Antony - that is correct, was going a bit fast there, of course you can name the windows. I changed it.
1

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"]

Comments

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.