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 Array Iteration Methods Array Iteration forEach() `index` and `array` Parameters

Why is mine printing empty array at the and?

const names = ['Selma', 'Ted', 'Mike', 'Sam', 'Sharon', 'Marvin'];
let sNames = [];
names.forEach((name, index) => console.log(`${index+1}) ${name}`));
// 1) Selma                                                                                        
// 2) Ted                                                                                          
// 3) Mike                                                                                         
// 4) Sam                                                                                          
// 5) Sharon                                                                                       
// 6) Marvin                                                                                       
// [] 

It seems so stange.

2 Answers

I copied your code into a workspace and ran it and it printed just the names as it should.

treehouse:~/workspace$ node app.js                                                       
1) Selma                                                                                 
2) Ted                                                                                   
3) Mike                                                                                  
4) Sam                                                                                   
5) Sharon                                                                                
6) Marvin    

Well, that's really weird. I feel like I'm hunted by a ghost. Thanks for checking!

Is this in the DevTools console on Chrome?

No, it was workplace in treehouse. Maybe it was a glitch I guess.

Juneau Lim ,

I found that what is wrong with your code.

You should remove 'console.log(sNames);' below the forEach loop.

This worked for me.