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
5,171 Points

You can also

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