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 and the DOM (Retiring) Making Changes to the DOM Appending Nodes

Why ul [0]?

If there is only one unordered list in the collection, why even need [0]?

Christopher Lebbano
Christopher Lebbano
15,338 Points

If you use javascript to link an element to a variable and it forms an array, even if there is only one index in the array you still have to specify with the [0] index.

2 Answers

Nathan Brenner
Nathan Brenner
35,844 Points

document.getElementsByTagName returns a node list, or as Guil has said previously a collection of nodes, which is basically an array but they don't inherit directly from Array.

I agree, if you know that there's only one node that would match that query, then it might be better to use document.getElementByTagName. I think Guil just used the alternative method to demonstrate the use more in this course.

Mars Epaecap
Mars Epaecap
5,110 Points

Maybe he plans on adding more unordered lists later ?