What's the difference between ` and ' in Javascript?

I’m working on the lessons for JS objects and I’m getting confused on ` and '. I would constantly use single quotes like ’ ’ for returning strings, and logging strings but sometimes the answers will tell me to use ` ` instead. When do I use each and what’s the difference between them?

Whenever you want to use quotation marks literally in your strings, use the template literals: `. Whenever you want to use placeholders ${your_variable}, use `.

1 Like

String literals with the backticks ( ` ) also allow you to put the variable placeholder right within the same string, instead of joining multiple strings together with the " " + variable + " " format.

When you have longer text and multiple variables inside some find it easier to visualize how the output will be by using template literals. It is also considered safer as is a character that is less common than the ’ or ", making it harder to introduce bugs when entering common text. They also allow your text to span across multiple lines including the white space and carriage returns just as you see them in your code.

let age = 7;
 
// String concatenation
'Tommy is ' + age + ' years old.';
 
// String interpolation
`Tommy is ${age} years old.`;

Some hints or answers may use template literals and others concatenation or both. But is really up to you (or the styling guidelines set by your company) to decide.