Is there a difference in using one or the other?

Hi, I’m new to javascript and I have this two loops in arrays (I got them from codecademy btw).

const vacationSpots = [‘Bali’, ‘Paris’, ‘Tulum’];

for (let i = 0; i < vacationSpots.length; i++){
console.log(‘I would love to visit’, vacationSpots[i]);
}
for (let i = 0; i < vacationSpots.length; i++){
console.log('I would love to visit ’ + vacationSpots[i]);
}

Both of them apparently print the same, but I notice that using the + operator I have to add a space in order to print it properly, with the comma I don’t have to though. I wonder if there’s any reason I should use the one instead of the other.

Console logging strings separated by a comma puts a space between them, but concatenating strings using the + operator does not, so "a" + "b" would give "ab", that’s why a space is needed to keep them separated.

1 Like

Ok, I understand, thanks, but is there any reason to concatenating the strings instead of just logging them separately?

Not that I know of, at least as far as logging goes

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.