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

How can I use the alert() method to display the third item of the snacks array.

How can I use the alert() method to display the third item of the snacks array.

script.js
var snacks = [ 'Pizza', 'Shawarma', 'Cookies'];
console.log(snacks[0])
alert() [2]
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Arrays</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

1 Answer

Brendon Butler
Brendon Butler
4,254 Points
alert(snacks.length + " snacks in array");

EDIT: Sorry, I misread the question

alert(snacks[2]); // you do the number you want minus 1 since it starts at 0