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 JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

Roger Hwang
Roger Hwang
3,851 Points

Using HTML tags in Javascript

When adding HTML tags to your site using JS, it works using just the starting tag without needing the closing tag. Is it better practice to include the closing tag too in JS?

Ex. this would create a list even without the closing tag: for (i = 0; i < correct.length; i++) { message += '<li>' + quizList[correct[i]][0]; }

1 Answer

Steven Parker
Steven Parker
231,275 Points

Each tag has its own rules about start and end tags. Some require both, some don't have an end tag, and for some it is optional. Here's what MDN says about the "li" tag:

The end tag can be omitted if the list item is immediately followed by another <li> element, or if there is no more content in its parent element.

It would still be a "best practice" to include end tags when they are optional.