Differences between interpolation and just stating the variable?!

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-javascript/lessons/variables/exercises/string-interpolation?action=resume

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
No error messages. Just wanted to know the differneces between my codes as the output is “nearly” the same:

```

console.log(“I own a pet " + favoriteAnimal + “.”);
console.log(“I own a pet”,favoriteAnimal,”.");

<do not remove the three backticks above>


Thank you

Console.log will print all of the arguments you provide it to the console separated by a space, string concatenation (does anyone actually call it interpolation?) allows you to be more explicit with how you join what you want to print.

For the purposes of the exercise they’re identical though I found that you actually DO get an error if you don’t use '+'s.

1 Like

Interpolation and concatenation are two different things.

console.log(‘My name is BlenderBoy and I’m the super hero of codeacademy and I’m here to solve all your problems’)

This is my bad, I recently hammered through this course so I could help people with their JS and when I came to this exercise I was surprised as I thought the + operator when applied to strings was considered concatenation.

Now I understand the following:

const str1 = "This is an " + "example of " + "concatenation.";

const str2 = "with strings";
const str3 = "Mixing variables " + str2 + " is interpolation.";

const str4 = "'+' operator interpolation";
const str5 = `Template literals streamline ${str4}.`;
2 Likes

You’ve got it.
I also admire the effort you put in to explain it. (I fell short there!)
Cheers!

1 Like

Thanks a lot for the information. I´m not sure about the details but I now see that “+” is the way to go for interpolation.

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