Javascript, difference between string interpolation and separating stuff with commas in console.log


#1

Hi,

this is the piece of code I have a question about

var favouriteAnimal = “Bird”;
console.log(“My favourite animal is the” , favouriteAnimal);
console.log("My favourite animal is the " + favouriteAnimal);

they both output the same thing, one is using commas, one is string interpolation? I’m super super new to this - i’m guessing in this case they do the same thing, but the operators (comma and plus sign) can do very different things?


#2

log() is a method of the console object, primarily used for debugging and inspection of variables and states. It takes a comma separated list of objects to render as text in the console. This is not concatenation or interpolation but simply passing multiple arguments to the method for output to the screen. Everything that is given to the method is treated as plain text.

String interpolation takes two forms, concatenation, and template literals.

"some text " + aVariable + " some more text " + anExpression + "!"

`some text ${aVariable} some more text ${anExpression}!`

#3

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