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 Loops, Arrays and Objects Tracking Multiple Items with Arrays Using Array Indices

How do I? Print Array variables in console.log()

var players = ['Jim', 'Shawna', 'Andrew', 'Lora', 'Aimee', 'Nick']; console.log(players[0,5]);

I thought this would print 1)Jim 2)Nick

Why won't it?

script.js
var players = ['Jim', 'Shawna', 'Andrew', 'Lora', 'Aimee', 'Nick'];
console.log(players[0,5]);
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

5 Answers

Shayne Laufenberg
Shayne Laufenberg
4,213 Points

You can just console.log the variable like how I have below.

Solution:

var players = ['Jim', 'Shawna', 'Andrew', 'Lora', 'Aimee', 'Nick'];
console.log(players);
Valeshan Naidoo
Valeshan Naidoo
27,008 Points

You have to have another console.log for the other name. You've correctly identified it's position, so you just have to place it in its own console.log line. Arrays, at least in JS, don't work like how you've structured it, your code will just produce the last listed position number on your call, so players[0,5] will return just 'Nick', as would players[3,5].

Thanks community , you guys are always so helpful

const players = ['Toni', 'Anwar', 'Mali', 'Carlos', 'Cormac', 'Sariah']; console.log(players[0]); console.log(players[5]);

// This worked for me, the instructions say on the second part to add another console log after the first one. I also got it wrong a few times and I went back to the instruction again and finally figured it out. hope it helps

Konrad Traczyk
Konrad Traczyk
22,287 Points

Best way to print your arrays is using console.dir instead of console.log, in node.js you cant print array with console.log

console.dir(players);

Cheers