Difference between using , and +


I’m just wanting to get clarity around interpolation.

Early in the exercise, we learn to use the comma to link the words in quotes to the variable:
var strength=‘50,000 pounds’;
console.log(‘How much stuff can a variable hold?’, strength);
This returned:
How much stuff can a variable hold? 50,000 pounds

Then, we went on to learn about using the + operator to interpolate a variable into a string:
var favoriteAnimal=‘dog’;
console.log('My favorite animal: ’ + favoriteAnimal);
This returned:
My favorite animal: dog

My impression is that in the above exercises, using the + operator and separating with a comma function the same way. But that’s not true, right? Please explain…

Many thanks!

, is a comma separator which is valid. However, it is not an operator beyond the fact that it inserts a space for every comma in a console.log() parameter list.

 console.log('this', 'will', 'output', 'like', 'a', 'sentence')

// this will output like a sentence

but it is not a sentence, only a list of strings.

console.log('this' + 'will' + 'output' + 'like' + 'a' + 'single' + 'word')

// thiswilloutputlikeasingleword

The text will be submitted as a single string.

1 Like

Thanks, this feels clearer now! :slight_smile:

1 Like