Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics Working with Strings Introducing Strings

Part of the video is learning how to show quotes in a string ""

I know template literals aren't shown at this point but to encourage people that later on there is a quicker way, you could also research template literals.

My code:-

const message = "I'm a programmer!" ; surround "I'm a programmer!" with backticks ``
console.log(message);

The above shows "I'm a programmer!" in the console.

2 Answers

Steven Parker
Steven Parker
210,837 Points

You can also get the same result just as quickly by surrounding it with apostrophes '' ("single quotes").

Best practice would be to only use backticks to create a template literal when you will also be using one or more substitution tokens (something like ${name}) in the literal.

Never used single quotes, Guil says to choose either single or double and stick with it, so I stuck with double. However your right you do interpolate into the template literals. 👍

Steven Parker
Steven Parker
210,837 Points

I also generally use double quotes. But in this case the string contains double quotes, so this would be a good place to use single ones to enclose the string.

You can still enclose the whole thing in double quotes, but you'd need to escape each of the quotes that is part of the content by preceding it with a backslash ("\").

Tony Idehen
Tony Idehen
4,952 Points

Better to use either single or double quotes but put a backslash after I ie " I\'m a programmer!"; or ' I\'m a programmer!';

Steven Parker
Steven Parker
210,837 Points

The backslash escapes the character behind it (in this case the apostrophe). But you don't need them in this example since the surrounding quotes use a different character.