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

Concatenation of single Quotes inside Double Quotes

Could Some one Explain to me how this concatenation works it is very confusing

<div> <img src="img/' + planet.name + '.jpg" alt="' + planet.name + '">' + </div>

1 Answer

Steven Parker
Steven Parker
229,732 Points

I would not expect that to "work". For one thing, it's incomplete. There's no statement there, only what looks like part of a concatenated string. Also, the single quotes are unbalanced — there's an odd number of them.

But it looks like the idea was to enclose the string bits in single quotes, and some of those bits have double quotes inside them. For example, in this complete statement with a modified version of this string, you can clearly see by the syntax coloring which parts are literal strings:

var sample = '<div><img src="img/' + planet.name + '.jpg" alt="' + planet.name + '"></div>';