Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
229,732 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
229,732 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,558 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
229,732 Points

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