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 AJAX Basics (retiring) AJAX and APIs Displaying the Photos

Kirill Kudryaev
Kirill Kudryaev
10,298 Points

How this syntax work?

Hello everybody, please help. I don't understand why we use this syntax, namely, two pairs of quotes and two pluses href="' + photo.link + '". Thanks for help.

1 Answer

Iain Diamond
Iain Diamond
29,379 Points
photoHTML += '<a href="' + photo.link + '" class="image">';

What's happening here is you're trying to embed a link variable, photo.link, within an HTML string.

Say for example the photo.link was set as http://www.someimagefile.com/myimage.jpg, you want to end up with the following HTML:

<a href="http://www.someimagefile.com/myimage.jpg" class="image">';

It's important point to remember is if you start a string in JS with a single quote, the interpreter will expect the string to end when it finds another single quote. Thus, you can embed double quotes within a single quoted string. The same holds true for embedding single quotes within double quoted strings.

In this way it's possible to use strings in JS that contain quote marks, by using the alternative quote version. E.g.

  • Embedded Double Quote ' " '
  • Embedded Single Quote " ' "

Hope this helps, iain

So what you're doing here is converting a link variable into a string in order to insert it? Without the single quotes it wouldn't be recognized as a string and instead what would be happening?