Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Foundations Arrays Getting and Setting

On about line 18, set the 31st element in 'myArray' to the word 'treehouse'.

My code is

var myArray[30] = "treehouse";

but I am getting an error that saud my var thirdElementInArray is not being passed..

3 Answers

The variable myArray has already been declared on line 16, so you shouldn't be using the var keyword. Remove it and you'll pass the challenge.

So you can only declare a variable once? If I re-use the same variable name towards the end of the script it doesn't override it?

Also by taking the variable syntax out, then what is myArray[30]? Is it still just an object with an index of 30?

The var keyword is only used when declaring the variable for the first time. Whenever you assign or reassign values to that variable, you shouldn't be using the var keyword, just the variable name.

var myVariable; // declares that there is a variable called myVariable
myVariable = 10; // assigns value 10 to myVariable
myVariable = "new value"; // assigns the string "new value" to myVariable instead of 10

myArray[30] is the 31st element of the myArray array, and that's still a variable, but JavaScript knows it's a variable, you don't need to tell it every time, only the first time.

I get it. Thanks!