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 Basic and Multiple Line Strings

Nicholas Gaerlan
Nicholas Gaerlan
9,501 Points

Technically, couldn't you still just use normal quotation marks in the example?

Out of curiousity... since both JS and HTML ignore whitespace (in the same way?), wouldn't the example shown still work if you substituted the back-tick for normal quotation marks? There's probably some other reason to use them, but it seems like the value would be the same in both cases.

3 Answers

Steven Parker
Steven Parker
216,165 Points

An interpolation template must be enclosed in accents/backticks for the substitution to work. If you use quotes instead, you are creating a normal string literal and the entire string is taken as-is with no substitution performed. Examples:

var val = "testing"
alert(`We are ${val}`);    // this alert will show "We are testing"
alert("We are ${val}");    // this alert will show "We are ${val}"
Nicholas Gaerlan
Nicholas Gaerlan
9,501 Points

Haha, the very next video shows it. But in the first part there's a <ul><li> block of code with quotations and concats, i think in that specific example, the use of temp lit syntax would still work with regular quotes since there was no interpolation of any sort happening.

Steven Parker
Steven Parker
216,165 Points

That's correct. If no interpolation is involved, normal quotes will do. I actually consider it a "best practice" to use accents/backticks only when template strings are required, and normal quotes otherwise.

andren
andren
28,521 Points

While Steven is correct that the main use of template strings is their interpolation ability, it is not the only use they have. And the example shown in this video would not in fact have worked with regular strings.

While it's true that JavaScript ignores whitespace for the most part it does not do so within a string. If you break a single string across multiple lines you will receive a syntax error as strings are only meant to span a single line of code. Template strings on the on the other hand does support multi-line content, and will include the whitespace within the generated string.

As an example here are two code snippets:

"Hello
World!"
`Hello
World!`

If you paste those into chrome's console or include them in a js file you will receive a syntax error for the first example but not for the second one.

Steven Parker
Steven Parker
216,165 Points

Good point. I answered the question without watching the video and didn't realize line breaks were involved. :wink: