Differences between interpolation and just stating the variable?!


#1


https://www.codecademy.com/courses/learn-javascript/lessons/variables/exercises/string-interpolation?action=resume


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,".");

Thank you


#2

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.


#3

Interpolation and concatenation are two different things.


#4

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


#5

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}.`;

#6

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


#7

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.


#8

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