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

Roger Hwang
Roger Hwang
3,850 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
210,871 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.