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 Arrays Loop Through Arrays Array Methods

terry okey
terry okey
3,187 Points

Assigning a "joined" string to a new var?

I have extensive notes but no mention of how to join arrays and then console.log them? Need new variable?

terry okey
terry okey
3,187 Points

I have this code but it says it is not logging the new string to the console?

const planets = ['Earth','Mars','Saturn','Mecury','Jupiter','Venus','Uranus','Neptune']; planets.join(', ') ; console.log(planets);

terry okey
terry okey
3,187 Points

Even the Mozilla DN page has no info on how to access a newly joined string?

1 Answer

terry okey
terry okey
3,187 Points

Sorry I found the answer finally on MDN. i was not expecting to put the join method right inside the console statement. I am still thinking in one move at a time.

const planets = ['Earth','Mars','Saturn','Mecury','Jupiter','Venus','Uranus','Neptune']; console.log(planets.join(', '));

Tom Lamb
Tom Lamb
9,472 Points

You can also

const planets = ['Earth','Mars','Saturn','Mecury','Jupiter','Venus','Uranus','Neptune'].join(', '); 
console.log(planets);