Soorya Ajeesh
Soorya Ajeesh

Reputation: 1

Javascript Array variable name

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.

Upvotes: 0

Views: 230

Answers (2)

adeneo
adeneo

Reputation: 318182

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.

Upvotes: 2

nalinc
nalinc

Reputation: 7425

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

Upvotes: 1

Related Questions