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 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?