Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Array Iteration Methods Array Iteration forEach() `index` and `array` Parameters

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

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

Seth Kroger
Seth Kroger
56,392 Points

Is this in the DevTools console on Chrome?

Juneau Lim
Juneau Lim
13,361 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.