Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

String interpolation quiz question

After watching "Getting Started With ES2015" under " Using Template Literals" there is a quiz portion that asks what the output would be from:

let name = "Smith";
function formalName(title, name) {
  return `${title} ${name}`;
}
console.log(`Hello ${formalName("Mrs.", name)}. How are you?`);

and at first I thought the answer would be:

Hello Mrs. name how are you?

but the correct answer is:

Hello Mrs. Smith how are you?

I'm stumped and trying to understand the logic. From a top down perspective I think the setting name with Smith is causing it to continue when calling it into the function with console.log? Am I correct that because name was set if the answer were to be Mrs name the function would need to have passed console.log(Hello ${formalName("Mrs.", "name")}. How are you?);?

If someone were to pass ${formalName("Mrs.", "Foo")} the answer would be Hello Mrs. Foo how are you??

1 Answer

You're absolutely right. The name variable was already defined, so they're just passing the variable (which contains the string 'Smith') into the function.