Codecademy Forums

FAQ: Variables - String Interpolation

Wow, it did ask for “favorite.” okay. i passed. thank you again!!!

1 Like

as there is string concatenation why we again going for string interpolation

String interpolation makes things so much simpler.

  • only one pair of quotes (back-ticks)
  • embedded (interpolated) variables and expressions
  • multiple lines within the quotes (pre-formatted text)
  • treated like strings so can be assigned or be a return value
let r;
r = prompt("Enter a radius")
console.log(`A circle of radius, ${r} has a diameter, ${2 * r}, a circumference, ${Math.PI * 2 * r}, and an area, ${Math.PI * r ** 2}.`);

Without string interpolation we would need four pairs of quotes and seven plus signs, and would need to be mindful of white space. Lots of room for error which the above clearly avoids.


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