How do you add spaces between the elements when console.logging? **Answer**

Hi all.

I’m not sure if this will help some of you, but if you’re like me and like to have your code and terminal output organized and look even, I have found a solution (after many searches-which wasted time).

If your console.log has output with list and you don’t want space each item, but only individual lists. just do this:

This is the lesson I was working on:

let spaceship = {
crew: {
captain: {
name: ‘Lily’,
degree: ‘Computer Engineering’,
cheerTeam() { console.log(‘You got this!’) }
},
‘chief officer’: {
name: ‘Dan’,
degree: ‘Aerospace Engineering’,
agree() { console.log(‘I agree, captain!’) }
},
medic: {
name: ‘Clementine’,
degree: ‘Physics’,
announce() { console.log(Jets on!) } },
translator: {
name: ‘Shauna’,
degree: ‘Conservation Science’,
powerFuel() { console.log(‘The tank is full!’) }
}
}
};

for (let crewMember in spaceship.crew) {
console.log(List 1: ${crewMember} : ${spaceship.crew[crewMember].name} )
}

console.log(‘\n’)

for (let crewMember in spaceship.crew) {
console.log(List 2: ${spaceship.crew[crewMember].name} : ${spaceship.crew[crewMember].degree})
}

And this was my output:

List 1: captain : Lily
List 1: chief officer : Dan
List 1: medic : Clementine
List 1: translator : Shauna
List 2: Lily : Computer Engineering
List 2: Dan : Aerospace Engineering
List 2: Clementine : Physics
List 2: Shauna : Conservation Science

But I wanted space between List 1 and List 2. Adding (\n) before List 1/List 2 in my console.log just added space between every item on List 1 and List 2.

If you look at the above code, I added console.log(‘\n’) with single quotation marks between the for…in loops and got this output:

List 1: captain : Lily
List 1: chief officer : Dan
List 1: medic : Clementine
List 1: translator : Shauna

List 2: Lily : Computer Engineering
List 2: Dan : Aerospace Engineering
List 2: Clementine : Physics
List 2: Shauna : Conservation Science

Okay, hope that helps some of you!

1 Like