Basic JS array confusion

Hi,
I am playing with arrays in js trying to learn them.

I created on array of three elements but when I try to call one of them I get the character not a whole element. I don’t understand why this is.

Why is name[2] t and not owl ?
Thanks,
Shane

It’s finding the “t” in cat.

c=0
a=1
t=2

Perhaps you meant something like this?

http://codepen.io/ryanreese09/pen/dPLjwV

Hi,
Thanks for looking at this. Why is it putting all the elements together into one string and not considering them as three separate strings?

Here I did exactly the same for an array called dinos and it works as i would expect.

var dinos = ["T-Rex", "Velociraptor", "Hugeasaurus"];
undefined
dinos
Array [ "T-Rex", "Velociraptor", "Hugeasaurus" ]
dinos[2]
"Hugeasaurus"
dinos.length
3
name
"cat,dog,owl"
var names = ["cat", "dog", "owl"];
undefined
names
Array [ "cat", "dog", "owl" ]

Thanks

Is it works on jsfiddle but when i use alert(name[2]); I get

I would have thought that element 2 in the name array should be owl and not the element 2 in the string.

Thanks,
Shane

I think it’s happening because name is an html attribute and something in the console is making it a string, because a name is a string. That’s all I can come up with. I’m really just making an educated guess.

SO Question

1 Like

I think mawburn is right. Consider:

var name = ["cat", "dog", "owl"];
console.log(typeof name); // string
(function () {
var name = ["cat", "dog", "owl"];
console.log(typeof name); // object
}());
2 Likes

Yes that sounds correct. i was checking to see if name was a reserved word. To html attribute can not be used for variable names in js.
Thanks for your help,
Shane

It does work for ‘title’ which is an HTML attribute but not for ‘name’.

var title = ["Mercury", "Venus", "Earth"];
undefined
title
Array [ "Mercury", "Venus", "Earth" ]
title[2]
"Earth"

An array is an object in JS, correct?

Name is a reserved word. Title isn’t.

Source - http://www.javascripter.net/faq/reserved.htm

1 Like

I checked the list on MDN and it isn’t there.

I see on your link it says

you’d better avoid
the following identifiers as names of JavaScript variables.
These are predefined names of implementation-dependent JavaScript objects,
methods, or properties (and, arguably, some should have been reserved words):

So name isn’t actually a reserved word but needs to be avoided.
Thanks,

hey where did that link to jsfiddle go that someone, can’t remember who posted here? Because that didn’t see ‘name’ as a problem reserved word type. Which surely it should have.
Post #4 has been removed.

That was me. I thought I misunderstood the question. :smile:

The code was simply:

var name = ["cat", "dog", "owl"];
alert(name[2]);
1 Like

And it gave “owl” out not “t”. Which suggests name works as an array name.

It looks like any property of the window object will cause problems. If you name your variable navigator or pageXOffset, for example, then you’ll also run into problems.

Moral of the story: Don’t use globals. Ever. You’ll run into naming clashes. Any of these variable names will be fine in some local scope.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.