FAQ: Variables - String Interpolation

This community-built FAQ covers the “String Interpolation” exercise from the lesson "Variables ".

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise String Interpolation

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

3 posts were split to a new topic: Why doesn’t this work?

13 posts were split to a new topic: Why should we use template literals?

A post was merged into an existing topic: Why doesn’t this work?

9 posts were split to a new topic: What am I missing from the string?

Do template literals appear in other programming languages, like Python?

Yes, though the name may be different. String formatting is the concern in Python. The aim is the same. Give a string representation of the data.

When using the single Template literal, why use the sign? What is the difference between coding console.log( 'Hello my name is' {myName}) and console.log(‘Hello my name is’ {myName})

It doesnt show the dollar sign in front of the first one for some reason??

Tip: When posting code to these forums, be sure to use the </> button directly above the text box. If you don’t, your code will be harder to read.

With </> button:

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

Without </> button:

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

1 Like

3 posts were split to a new topic: Did I spell favourite wrong?

Hi,

my code

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

seems to be correct, the result shows up correctly, but it won’t let me keep on working.
What’s wrong?

Hello, @hans-wernerweisskirc.

Welcome to the forums.

The SCT for the exercise is expecting the string to be exact. It appears you capitalized City. should just be city.

Thanks…of course… :man_facepalming:…that happens when your native language is German.

1 Like

I am unable to connect to Codecademy when I get to this page - String Interpolation 8/10

I have already commented on another thread about this issue. Codecademy works fine everywhere else I have gone it seems but when I try to continue my course in JavaScript it is unable to connect at this page. Please fix as I cannot progress any further with this issue. Thank you.

This may be a dumb question, but what if you need to use the backtick within the string?

For example, in my native language we call it “crase” and its’s regularly used in writing.
Ex.: Voltei à bela Porto Alegre.

Is there a solution for this, or are we portugese speakers (writers) bound to concatenation?
That would’nt sound very accessible…

The backslash is an escape character.

To use a backtick in a template literal(which is surrounded by backticks) you could use \`

If you are trying to use a backtick in a string surrounded by single or double quotes, you would enter the backtick without the need for the backslash.

1 Like

That’s not a back tick, its a grave accent. For that we have Unicode, or in HTML we could use enitities… &agrave;.

1 Like

I noticed you don’t have to type let, const or var like my example below. I assume it’s just not good practice to code this?

myName = “DeFiDegen”

myCity = “Montreal”

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

Scoping variables is good practice, especially if we are conscious of why and where we are scoping them to. Written as they are in your example is okay for working in global scope only, but once we get into a function then we can run into problems. All non-scoped variables at any level of function nesting will automatically become properties of the window object and be exposed to the whole program. This can have disastrous effects.

1 Like

Thank you for the clarification!

1 Like