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 Quickstart Arrays and Loops Create an Array

What am I doing wrong?

I don't know how my code is wrong.

script.js
var snacks = ["chips", "popcorn", "crackers"]
console.log(snacks[0]);

alert(snacks[4]);
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Arrays</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

4 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hi Arthur,

You're doing great.

But the 3rd task says to alert() the third item. Right now, your code is trying to alert() the 5th item, which doesn't exist. Remember, arrays use zero indexing, so the 3rd item with be at index 2.

Keep it up! :) :dizzy:

Natalie Tan
Natalie Tan
25,519 Points

Javascript array starts with 0 index.

For your array

var snacks = ["chips", "popcorn", "crackers"]
  • chips is at 0 index and is accessed by snacks[0]
  • popcorn is at 1 index and is accessed by snacks[1]
  • crackers is at 1 index and is accessed by snacks[2]

snacks[4] does not exist in your array

Brandon Evanson
Brandon Evanson
9,482 Points

If you count the snack list starting at 0, the third snack would be 2

Oh! I'm stupid... Thanks guys!

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

Nah, never "stupid". And don't worry... we all have and still do make silly mistakes every now and again. It keeps us on our toes. :smile: :thumbsup: ?‍♂️