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!

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

Juneau Lim
Juneau Lim
13,362 Points

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    
Juneau Lim
Juneau Lim
13,362 Points

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

Seth Kroger
Seth Kroger
56,412 Points

Is this in the DevTools console on Chrome?

Juneau Lim
Juneau Lim
13,362 Points

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

Hakan Ergoksen
Hakan Ergoksen
2,168 Points

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.