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.

1 Like