Why isn't string interpolation working? (Check for backticks)

Why isn’t my interpolation working:
console.log(‘My name is {myName}. My favorite city is {myCity}.’);
The syntax looks right. It just prints the literal characters ${myName} without reading them as a variable.

It seems to be bugged. If I type it, the console doesn’t recognise the variable as a variable, but as a string. If I copy and paste it, it recognises it as a variable - even though, to me, it appears as the same text.

1 Like

A template literal is enclosed in back-ticks and interpolation is performed on expressions with the syntax, ${expression}.

10 Likes

Thank you. I was missing the back-ticks, they look very similar to single quotation marks.

6 Likes

Hey Guys I tried the whole String Interpolation-Thing.

I did this:

console.log(My name is ${myName}. My favorite City is ${myCity}.)

and get this notification:

Did you interpolate myName and myCity using back ticks and ${} ?

But at on the right side the right ‘text’ is showing?
Could that be a bug because I am using a german keyboard?

I will just click on ‘solution’ for now, because I think I got it right, but would be awesome if you could help me.

Greetz Jaschi

EDIT: As I can see there are now backticks shown, but I did them. There are shown on my screen when I type and edit stuff. Lol Wtf?

4 Likes

Hi, i have some practical experience coding, I am not completely new to this. That said, I have noticed the check that determines whether the final answer is acceptable is a bit buggy. I had the following code entered into my console for over 10 tries and it would not be accepted by the page. It turns out I had a space at the end of the line and that was preventing the correct answer from being accepted… I was very frustrated to say the least.
console.log(`My name is ${myName}. My favorite city is ${myCity}.`);

AHA, finally figured out how to post a backtick ( ` ) and a dollar sign ( $ ) here!
@mtf I corrected the displayed code to match what I was indeed typing already. The interpreter didn’t like it originally and I think it was because of a non-visible character.

Recall that the interpreter needs to be alerted to interpolation. The above will display,

My name is {myName}. My favorite city is {myCity}.

Everything is already contained in a string, meaning they are all printable characters. We need to tell the interpreter there are variables in this string.

`My name is ${myName}. My favorite city is ${myCity}.`

The $ is very important since it tells the interpreter to evaluate the expression in the curly braces, and to not print them, only the outcome.

1 Like

On this platform, there is a tool in the post editor tool tray, </> that we can click to apply to a block selection. Paste in the code, leaving an empty line above it and below; select the entire block then click that button. That will give us block formatting. It’s the same as typing three back ticks on the top line (by itself) and three after the last line.

Inline code can be denoted with a single back tick before and after.

There is a post in the New User section that discusses this in detail.

1 Like

Hi folks,

Like others, I am not seeing the error in my string. I have used back ticks. I used ${} for the

let myName = ‘DJ Jazzy Jeff’

let myCity = ‘Coolville’

console.log(My name is ${myName}. My favorite city ${myCity}.)

Can anyone see what I am doing wrong?

I’m going right out on the limb here to criticize the grammar.

My name is ${myName}.

Perfect, save the missing comma.

My favorite city ${myCity}.

Somewhat at a loss.

Check your second sentence… its missing an expected word. Same thing happened to me. :slight_smile: LOL… speaking of “LITERAL”.

let myName =‘Harsh’;
console.log(myName);
console.log(“My name is ${myName}.`”);

This was my code, I checked for backticks, still its not working
It ignore the dollar sign ($) it seems
Please help

console.log(“My name is ${myName}.`”);

You only need the back tick symbols to complete the string literal, no need for the single quote nor double quotes. You already have one back tick, just add the other. Review the example in lesson 8 and compare to your code

back ticks were an easy miss for me too!

I need help!
I can’t see what is wrong here,

let myName = “Cindy”;

let myCity = “Limerick”;

console.log(‘My name is ${myName}.’);

console.log(‘My favorite city is ${myCity}.’);

It keeps showing this msg " Did you interpolate myName and myCity using back ticks and ${} ?" I can’t go aheaddddddd!

Hello @blogblaster57419 and welcome to the Codecademy Forums!

When we create template literals, we must surround them with backticks ( ` ), which can be found just below the escape button on your keyboard.

See here for more information.

Still not working… =/

1 Like

Currently, your code outputs:

My name is Cindy.
My favorite city is Limerick.

However, Codecademy wants the output formatted in one line, like this:

My name is Cindy. My favorite city is Limerick.

Thank you for answer me but I did what you said it’s everything all right and still negative I don’t understand why =/

Nowwww I got it! Finally!!!

var myName = “Cindy”;

var myCity = “Limerick”;

console.log(My name is ${myName}.My favorite city is ${myCity}.);

It must be shorter!

It was difficult at the beginning, but I saw my mistake, and it may help you.
The main point is --> single quote ( ’ ) is not equal to back-tick ( ` )

You must use back-ticks, ok?

'My name is {myName}' is not equal to `My name is {myName}` <<<<< (this is what you should use).

Just done this, and i had it all typed out and i kept getting the error. I reset the entire thing typed it all out again exactly the same then it worked.
That fried my brain for today :smile: