FAQ: Variables - 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.

2 Likes

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

The way I conceptualized this exercise is that we can use the back-tick to create a string out of combined variables. Instead of having to worry about adding quotes on numbers and not on strings, we simply turn the whole statement into a string, and simply create a container for the variable names ${myVariable}

Bear in mind that not only variables, but expressions may be written in the placeholder.

Thanks, I’d like to ask another question, unrelated to this matter, if you don’t mind.

I am taking this course in beginning JavaScript, as well as working through some other guides online. I can tell that in the future, to really be able to really implement the full power of JS there will be math involved. I am an older student (53) and my math skills are rusty (which is an understatement.)

If I wanted to take some remedial classes alongside this course, do you recommend any? Which math skills should I re-learn that are most common to writing code?

There is some overlap between programming and math, but not a large prerequisite syllabus.

If you have time and patience and are willing to put in the hours, Khan Academy has an excellent Math module from elementary right up to college.

We don’t necessarily need any math skills to learn basic programming. Syntax, variables, data types, operations, loops and basic logic can all be learned without knowing arithmetic. However, some of the problems do involve simple math.

+  => addition
-  => subtraction
*  => multiplication
/  => division
** => exponentiation
%  => modulo (remainder)

If you can get your hands on some middle school (Gr. 7, 8, 9) math texts and go through them to pick up the basics on algebra and geometry, that would be a good place to start. In Canada we have GED, which book is also a good examination of core arithmetic/mathematics concepts.

Ultimately, programming allows us to use the computer to handle math operations of all sorts, depending on the objective of the program.

Once you get a handle on the core algebra and geometry concepts, look to cover the basics of trigonometry. It’s a real eye-opener, but from my experience a lot of people struggle with it.

Bottom line, push ahead with learning the basics and if a math problem comes up that you do not quite understand, we’re here to help.

1 Like

Dear guys.

This is my code:
let myName = “John”;
let myCity = “NYC”;
console.log(My name is ${myName}. My favourite city is ${myCity}.);

Note: the backticks ` are not visible in the console.log, but they are really there in my screen. It must be that the editor of the forum doesn’t allow them.

I get printed in the terminal: “My name is John. My favourite city is NYC.”

Nevertheless, I get an error message telling: “Did you interpolate myName and myCity using back ticks and ${} ?”

What is wrong?
Thanks for the feedbacks :smiley:

Being as we are using American English, there is no u in favorite.

1 Like