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 trialanalyn jane calado
3,523 Pointsthe starting variable of creating an array
should i used the var key word always, if i have to create an array?
5 Answers
Nathan Heffley
19,878 PointsTo create a new array called "shoppingList" that has the items "carrots" "milk" and "eggs" in it, you would use this code:
var shoppingList = ["carrots", "milk", "eggs"];
The var keyword is used because you are creating a new variable. An array is just a more complicated variable that what you are used to. You only use the var keyword when you are creating the array. Once you have created the shoppingList array you will not need to put the var keyword in front of it again.
Kathleen Sapp
12,776 PointsAnalyn- I am still learning Javascript but this is a quote from one of my coding textbooks: "An array is a variable containing multiple values. Any variable may be used as an array." So if an array is a variable then it would need to be identified as var.
Adrienne Cabouet
3,284 PointsHi Analyn, you should use always use the var keyword when creating a new array. It looks like this:
var fruits = [apple, banana, pear, strawberry];
analyn jane calado
3,523 Pointsin my workspace in this lesson, why it's not working?
Adrienne Cabouet
3,284 PointsFor that lesson, I was able to complete the exercise with this answer: var data = [1, 2, 3];
analyn jane calado
3,523 Pointsyeah i got it.. aahhmmm,..... when i asked u, i tried that answer, and it works!!
analyn jane calado
3,523 PointsI'm am now in the 2nd challenge,... i can't completely understand the question.
Susan Rusie
10,671 PointsI watched the video and was also having trouble with that one. Once I reviewed the video the answer came to me. If you do the following, you should pass the 2nd part of the challenge without a problem:
var assorted = ['apples', 99, true];
You can assign different values than what I have and it should work. Hopefully, this helps.
Jacob Mishkin
23,118 PointsJacob Mishkin
23,118 PointsAnalyn,
question 2. Leave the data array in place. Add another line of code that creates a second array named assorted. Assign one string value, a number and a Boolean value (like true or false) to the array. The complete assorted array should have 3 values total.
are you still having problems with this question?